开发者

How to isolate unit tests

We have a batch processing project that utilizes a cache (that relies on static data). The unit tests for this project unfortunately mess up other unit tests within the same solution (because of the static data), so I need to isolate the batch processing unit tests from all other unit tests. The framework/organization that we work in is really strict and all unit tests have to be in the same project. Still all unit tests have to be run in the build. What I need is to ensure that the batch processing unit tests are not run within the same process as other unit tests. Is there any way to do this?

Edit: Specifically, I'm looking for a way to control which process each test is run under. I couldn't find documentation about this, but it seems that all unit test are run under the same instance of the QTAgent32.exe process. There's a setting that determines whether tests are run in the same process (Tools > Options > Test Tools > Test Execution > Keep test ion engine running between tests). However, this is a global setting. What I need is to say is "run these tests in this process, and开发者_开发知识库 these tests in this process". Is there any way to do this?


That's the problem with global state, as you have mentioned: modifying it in one test, will affect other tests.

This tells me you are using automated tests more as an Integration tests. You can still create another project for all you "integration" style tests and place them together (knowing that they may affect each other and be brittle) and you can plug both test projects into your build / CI system.

The other way to fix this is to use mock objects and make write tests in "unit test" style, having no other dependencies than the Class Under Test. Depending on what you are writing this may range from being hard to do so, not possible or applicable by minor refactorings.


One key requirement for a good unit test is that it is isolated. In practice, this means no unit test uses state from other tests or can be affected by state in other tests.

A unit test must set up the required state before execution and tear it down afterwards. This can for instance be done through the use of test fixtures. Related to this is Arrange-Act-Assert (AAA), which is a pattern for writing unit tests.

In your case the static data is bad and that makes it difficult to test!

What about refactoring your code so that the static data is available through some kind of interface or through a factory?

Not knowing your problem area it's impossible to give a precise answer what would be the best. However, the approach you should go for is to take out the shared static data, so each test can set up it's own data set. Now each test can be isolated and will only deal with "own" data.

This gives further benefits such as 1) unit tests would just have a data set enough to test one thing 2) less dependencies in your system, and 3) probably faster/smaller tests.

Lastly, I agree with Hadi that you should keep unit test and integration/functional test separate. This is very important, because they test things differently.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜