Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special compiler flag for unit test runs

I have a question concerning visual studios built in test suite. Is VS studio running these tests with some special compiler flags applied?

The problem description is as following. I have overloaded the Equals function on one of my classes. During test runs it would be nice, if it could give me some additional information, which members in the class aren't equal at all.

Therefore I'd like to implement some messages only if the application is running in test mode.

Thanks for any reply! Andreas

like image 731
Andreas Walter Avatar asked Oct 24 '11 06:10

Andreas Walter


People also ask

How do you run a unit test?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

In which build phase are unit tests run?

Unit Testing is done during the development (coding phase) of an application by the developers. Unit Tests isolate a section of code and verify its correctness.

How do I run a specific test in Visual Studio?

All you have to do is: Right click your test function name. Select Run Test or Debug Test.


2 Answers

VS compiles/builds the projects with the currently selected build configuration. So a solution might be to create a separate build configuration yourself, and then use a constant (e.g. TEST) for the projects in that particular build configuration. The output method execution can then be restricted either with the #if TEST directive or with the [Conditional("TEST")] attribute. You could configure your build server te allways run the tests with that particual build configuration, so you would see additional output. You would however need to switch between the build configurations manually when running the tests from VS

like image 100
m0sa Avatar answered Sep 29 '22 20:09

m0sa


Create a new solution configuration "Test" (if you don't have it yet) and switch to it. Open project settings, switch to the Build tab and define a new symbol TEST. Press OK.

Change your Equals implementation to

public override bool Equals (object obj)
{
    #if TEST
     // Your implementation
    #else
      return base.Equals (obj);
    #endif
}

This will compile a different method body for your test configuration.

like image 38
Ivan Nikitin Avatar answered Sep 29 '22 20:09

Ivan Nikitin