开发者

Best practices for writing parallel unit tests

I'm looking into using parallel unit tests for our project(s) and was wondering about any best practices 开发者_JAVA技巧for actually writing such parallel unit tests.


If by parallel unit tests you mean tests that can run concurrently, the most important advice I can give you is to avoid so-called Shared Fixtures.

The book xUnit Test Patterns describe the term Fixture, which basically can be described as the entire context in which each test case executes, including persistent and transient data.

A Shared Fixture indicates that test cases share some context while running. If that context is mutable, race conditions may occur.

Keeping a Shared Fixture immutable (a so-called Immutable Shared Fixture) will allow you to run tests in parallel, but even better, so-called Fresh Fixtures (where each test case has its own Fixture) are thread-safe by definition, since only the test case itself has access to the Fixture.

Examples of Shared Fixtures include any sort of test that use a shared database, but also include tests where you have static in-memory state in either the System Under Test (SUT) or the tests themselves, so you need to avoid that.

You should also keep in mind that if your SUT accesses shared (static) data, that access itself must be thread-safe.


Some interessting Links for you:

Organize Unit Tests

Running Unit Tests in Parallel

There are some interessting Answers in this Stackoverflow Questions. Hope this will help.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜