Johan Venter Johan Venter - 2 months ago 18
C# Question

Run NUnit tests in release mode

I have a problem.

#if DEBUG
ca.ConveyancingAttorneyID = "C0B68DC3-C396-4264-880B-1A699C53A8CE";
ca.ConveyancingFirmID = "C0B68DC3-C396-4264-880B-1A699C53A8CE";
#endif


When we are running in debug mode, we hardcode the identifier. In release, it's obviously dynamically set.

Now when it comes to unit testing the mapping code, I've had to do this : (initally I just tested the 'Release case', which would then alwasy fail in Debug mode)

#if RELEASE
Guid id = new Guid("1A92CE5A-D956-486B-9B65-0A3630EF0285");
#endif
#if DEBUG
Guid id = new Guid("C0B68DC3-C396-4264-880B-1A699C53A8CE");
#endif


This is not ideal. And having to remember (and remind teammates) to always run the Unit Tests in Release is also a hassle. We don't have a continuous integration setup, so the tests are only ever run locally.

Is there a way to force NUnit / Visual Studio Testrunner / Resharper Testrunner to always run tests in Release mode? Or is there a better way to approach this, keeping in mind that these kind of '#if DEBUG' codeblocks are quite abundant in the solution.

Answer

I'm not sure we know enough about the structure of your project to answer this in an adequate way. It basically depends on where and how you are using these values.

Ideally, you should fetch these IDs from somewhere via an interface. That would let you to fake and switch the implementation behind that interface, allowing you to return hard-coded dummy values when needed to test the logic where the values are used.

You could set up a configuration option allowing you to specify whether to use the real implementation, or a dummy data provider to allow switching without having to recompile.

Update: Since the above was not really a great answer to your specific question, here's another attempt: Under Resharper > Options > Tools > Unit Testing, there is an option Run tests from:, where you can specify a folder to use instead of the default project output folder.

This may be a long shot, but perhaps you can configure VS to build a Release version of your project to some specific folder, and refer to that for testing, even if the main project build configuration is set to Debug?

Comments