Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSTest: How to change name of TestMethod in TestExplorer?

I am currently building a sample application where I use MS Test to implement a Scenario-based Given-When-Then-style UnitTest-project in VS2012. For this to work I have an abstract Scenario (base) class that has virtual Given()- and When()-methods that are run during the TestInitialization-phase. The results of the When()-method are then stored and can be verified using arbitrary TestMethods in any concrete Scenario-class, representing the 'Then'-statements. This all works perfectly.

There's one more thing I'd like to control, though: the names of all the TestMethods as they are shown in the TestExplorer of Visual Studio. This is because many TestMethods have the same or similar names, but are executed in different scenario's (such as 'ExpectedExceptionIsThrown'). I would have thought such a thing would be supported by MS Test, perhaps by native support of the TestMethodAttribute like so:

[TestMethod("DisplayName here...")]
public void ThenThisShouldHappen()
{
  ...
}

I've looked through the API of MS Test but can't seem to find any way to do this. Is this at all possible with MS Test?

like image 772
Wim.van.Gool Avatar asked May 01 '13 08:05

Wim.van.Gool


People also ask

Which attribute should be used to mark a method as test method in MSTest testing?

The TestClass attribute denotes a class that contains unit tests. The TestMethod attribute indicates a method is a test method.

How do I add a test in test Explorer?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

Which annotation identifies the method that is called just once after the last test in TestClass in MSTest is executed?

@After annotation is used on a method containing java code to run after each test case.

Does TestInitialize run for each test?

TestInitialize and TestCleanup are ran before and after each test, this is to ensure that no tests are coupled. If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize and ClassCleanup attributes. Save this answer.


1 Answers

I was struggling with this concept for awhile myself until I realize that you can right-click the Test Explorer area and choose Group By -> Class.

This isn't perfect by all means, but coupled with all of the functionality available with test playlists and/or Resharper, it is possible to customize your Test experience a bit.

like image 65
djrecipe Avatar answered Oct 08 '22 13:10

djrecipe