Question

diltsman on Tue, 09 Feb 2016 23:34:02


I have a codebase that uses global state for certain error handling functionality.  I am attempting to test code that uses the code that uses this global state.

When I run my tests one at a time, they all pass.  When I hit "Run All" about 40% fail.  I set the global state at the beginning of each test to something innocuous, but a few of the tests change that in the middle.  I assume these changes are causing the test failures.  It definitely appears that test interactions are causing the failures.

Since I cannot change the code that has the global state, it seemed like a possible hack of a solution is to create a global mutex and have each test lock it when execution starts.  It seems that this should effectively prevent multiple concurrent tests from running, but I am still seeing test failures.

Can anyone suggest how I might fix this?  Also, why does it not appear to be serializing test execution?

 

Sponsored



Replies

Jack-Zhai on Wed, 10 Feb 2016 17:29:49


Hi diltsman,

>>When I run my tests one at a time, they all pass.  When I hit "Run All" about 40% fail. 

If your test projects failed when you run all of them at the same time, one possible reason is that other tests impact certain tests during running time.

I'm not very sure that how you use the global state in your test code, but generally we often use the TestInitialize() and TestCleanup() for our tests.

Reference:

http://blogs.msdn.com/b/densto/archive/2008/05/16/using-a-base-class-for-your-unit-test-classes.aspx

So it would call the TestInitialize before every test ran, call TestCleanup() after one test finished.

For example, if you have a global variable or other parameters, you could define it in TestInitialize, so every test would call this variable before it runs, but like your previous comments, if the variable in one test will impact the result of another test, for example, the value has been changed, maybe you would clean up this variable in the TestCleanup method, so the value of the variable would not be changed before another test runs.

If I have misunderstood this issue, please feel free to let me know.

Thanks,

Jack


We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.