-
Notifications
You must be signed in to change notification settings - Fork 979
Testing Tips
Paul Rogers edited this page Nov 25, 2016
·
26 revisions
Drill makes extensive use of JUnit for testing. Most of us know the basics of JUnit. Drill uses many advanced features that we mention here.
Good references:
- JUnit Wiki, especially the Usage and Idioms section.
- Hamcrest Tutorial
- Hamcrest Java on GitHub
- Understanding JUnit method order execution. Good overview of how the before/after annotations work.
- See also the update to the above.
- Using JUnit with Maven
Drill tests use the JUnit 4 series that uses annotations to identify tests. Drill makes use of the "Hamcrest" additions (which seem to have come from a separate project, later merged into JUnit, hence the strange naming.) Basic rules:
- All tests are packaged into classes, all classes start or end with the word "Test". In Drill, most tests use the prefix format: "TestMumble".
- Test methods are indicted with
@Test
. - Disabled tests are indicated with
@Ignore("reason for ignoring")
- Tests use "classic" JUnit assertions such as
assertEquals(expected,actual,opt_msg)
. - Tests also use the newer "Hamcrest"
assertThat
formulation. The Hamcrest project provided a system based on assertions and matchers that are quite handy for cases that are cumbersome with the JUnit-Style assertions. - Many tests make use of the test fixture annotations. These include methods marked to run before or after all tests in a class (
@BeforeClass
and@AfterClass
) and those that run before or after each test (@Before
and@After
). - The base
DrillTest
class uses theExceptionRule
to declare that no test should throw an exception. - Few Drill tests verify exceptions directly. But, the cleanest way appears to still be the try/catch idiom.
- Drill tests have the potential to run for a long time, or hang, if thing go wrong. To prevent this, Drill tests use a timeout. The main Drill test base class,
DrillTest
uses a timeout rule to set a default timeout of 50 seconds:
@Rule public final TestRule TIMEOUT = TestTools.getTimeoutRule(50000);
- Individual tests (override?) this rule with the timeout parameter to the Test annotation
@Test(timeout=1000)
. This form an only decrease (but not increase) the timeout set by the timeout rule. - Tests that need a temporary file system folder use the
@TemporaryFolder
rule. - The base
DrillTest
class uses theTestName
rule to make the current test name available to code:System.out.println( TEST_NAME );
.
Some other resources that may be of interest moving forward:
- JUnitParams - a cleaner way to parameterize tests.
- Assumptions for declaring dependencies and environment setup that a test assumes.
- JUnit Rules may occasionally be helpful for specialized tests.
- Categories to, perhaps, identify those "smoke" tests that should be run frequently, and a larger, more costly set of "full" tests to be run before commits, etc.
- [System Rules][http://stefanbirkner.github.io/system-rules/] -
A collection of JUnit rules for testing code that uses
java.lang.System
such as printing toSystem.out
, environment variables, etc. - The
Stopwatch
rule added in JUnit 4.12 to measure the time a test takes. - the
DisableonDebug
rule added in JUnit 4.12 which can turn off other rules when needed in a debug session (to prevent, say, timeouts, etc.)
The root Drill pom.xml
declares a test-time dependency on JUnit 4.11:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
Since this dependency is in the root POM, there is no need to add it to the POM files of each Drill module.