Build/Test Issues

static variables inside classes

Started by tofutim on 5,364 views

Hi, I recently ran into an issue using NCrunch where static variables for the class are shared across multiple tests when running in parallel - even tests that are in different files.

For example, I have a class with

public static bool logFile

When the application starts, it is supposed to assign the logFile, and this is shared across all class instances. However, if I have two tests running, even with different logFile names, the logFile will be assigned by just one of the tests.

Is there anyway to compartmentalize the tests so that static is not shared? How do others handle this?
Hi, thanks for posting!

NCrunch will never, ever, ever run multiple tests in parallel within the same execution process. It really is physically impossible with the current design. Based on your description, I think it could be worth checking the following:

- Make sure that the log file name is completely different (i.e. randomly generated) for every test that is executed. It isn't enough to use a sequence counter in generating the file name - try using a Guid or something you can be sure will be 100% unique per test run.
- Make sure that the static variable causing the problem is not somehow sequence dependent, and that it is being definitively assigned at the beginning of the execution of each individual test.
- Make sure you don't have background threads that may be manipulating or making use of the static variable. An example of this could be a test that kicks off a thread to do some work which continues to happen after the test has finished execution on the primary thread - as this could cause the background thread to clash with any tests that are subsequently running later in the test run.

I hope this helps!


Cheers,

Remco

Post a reply

Log in to reply.