Visual Studio Test startup detection

Is there an easy way to determine if you are working in the context of a Visual Studio test, and not debugging or release?

Here's the scenario - we have a factory class that we use heavily in the entire existing code base, and I decided to reorganize it in each class instead, so that we can replace the standard factory with one that returns mock / fake objects, I could add something in the factory class to return these layouts if it detects that it is in test mode.

+3
source share
4 answers

I don’t think it’s nice to mix test code with domain code.

factory mocks .

, , ​​ RhinoMocks.

+4

mcabral ( , ), ...

Visual Studio , "Debug" "Release", , . , "" , , Debug. "" , factory.

"". "" : "".

#if TESTING
     // build stubs
#else
     // build real implementations
#endif
+1

Injection Dependency IOC? .

, IOC , factory. , factory IOC.

- ( ), .

+1

You can test the current executable file of the process. It is not very elegant, but it works.

if (Process.GetCurrentProcess().ProcessName.ToLower().Contains("vstest.executionengine"))
{
  // we are in a ms unit test
}
else
{
  // normal programm execution
}
+1
source

Source: https://habr.com/ru/post/1740022/


All Articles