9

In lots of tutorials for UnitTesting, the way to mark a TestMethod was different. I saw these options:

[TestMethod] 
[TestMethod()]

What is the difference?

1
  • 4
    [Test] attribute belongs to NUnit Api.
    – Ravi
    Commented Jan 22, 2014 at 9:34

3 Answers 3

14
+50

With and without brackets its exactly the same:

[TestMethod] 
[TestMethod()]

The empty brackets just call the default constructor of that attribute that has no parameters. So does [TestMethod]. Both call the default constructor.

This would be different:

[TestMethod(SomeParameter)]

And [Test] is an attribute that comes from NUnit library and is different from the .Net [TestMethod] attribute.

2
  • I used some [TestMethod] with parameter and used it in the TestMehod like: actual = target.TextToDouble(textBoxText); So, where and how should i use the SomeParameter? Can you give an example?
    – Johnny
    Commented Jan 22, 2014 at 9:45
  • For available parameters & properties check msdn.microsoft.com/en-us/library/…. You could use [TestMethod(TypeId = "test")] for example
    – RononDex
    Commented Jan 22, 2014 at 10:00
1

The empty parentheses are redundant, the two lines are equivalent. Tools such as ReSharper would give you the option of removing this redundancy from your code.

1
[TestMethod] 
[TestMethod()]

Both are same but when Visual Studio auto generate test method it comes with [TestMethod()]

Not the answer you're looking for? Browse other questions tagged or ask your own question.