-
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.
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.