Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why put parentheses on TestMethod attribute in UnitTest

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

[TestMethod] 
[TestMethod()]

What is the difference?

like image 974
Johnny Avatar asked Jan 22 '14 09:01

Johnny


3 Answers

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.

like image 133
RononDex Avatar answered Sep 28 '22 11:09

RononDex


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.

like image 42
Ian Nelson Avatar answered Sep 28 '22 09:09

Ian Nelson


[TestMethod] 
[TestMethod()]

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

like image 30
Damith Avatar answered Sep 28 '22 11:09

Damith