Difference between revisions of "QA/test automation rules"
(Created page with "=== Basic rules for JUnit 4 === '''Rule.''' Recommend to follow [http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html Java code conventions]<br /> …") |
(→Basic rules for JUnit 4) |
||
Line 1: | Line 1: | ||
− | == | + | {{DISPLAYTITLE: Best Practice Rules for JUnit 4}} |
+ | [[Category: Quality Assurance]] | ||
+ | [[Category: TestAutomation]] | ||
+ | |||
+ | == Best practice rules for JUnit 4 == | ||
+ | {{Template:Documentation/Caution|MUST READ THE SECTION BEFORE COMMITTING AUTOMATION CODE}} | ||
'''Rule.''' Recommend to follow [http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html Java code conventions]<br /> | '''Rule.''' Recommend to follow [http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html Java code conventions]<br /> | ||
'''Rule.''' Don't miss assertions. One test method should include at least one assertion, otherwise the test will always pass.<br /> | '''Rule.''' Don't miss assertions. One test method should include at least one assertion, otherwise the test will always pass.<br /> |
Revision as of 02:57, 15 August 2012
Best practice rules for JUnit 4
Rule. Recommend to follow Java code conventions
Rule. Don't miss assertions. One test method should include at least one assertion, otherwise the test will always pass.
Rule. Use assertions with an informative message.
Bad
assertEquals(expected , actual); assertNull(actual); assertNotNull(actual); assertTrue(actual);
Good
assertEquals("actual should equals expected", expected , actual); assertNull("actual should be null", actual); assertNotNull("actual should not be null", actual); assertTrue("actual should be true", actual);
Rule. Don't use assertTrue wrongly.
Bad
assertTrue("actual should be same with expected", expected == actual); assertTrue("actual should equals expected", expected.equals(actual)); assertTrue("actual should be null", actual == null); assertTrue("actual should not be null", actual != null);
Good
assertSame("actual should be same with expected", expected, actual); assertEquals("actual should equals expected", expected, actual); assertNull("actual should be null", actual); assertNotNull("actual should not be null", actual);
Rule. Use assertArrayEquals instead of assertEquals for array data.
Bad
assertEquals("actual[0] should equals expected0", expected0, actual[0]); assertEquals("actual[1] should equals expected1", expected1, actual[1]); assertEquals("actual[2] should equals expected2", expected2, actual[2]);
Good
String[] expected = {"0", "1", "2"}; String[] actual = someMethod(); assertArrayEquals("actual should equals expected", expected, actual);
Rule. Avoid negation in an assertTrue or assertFalse test.
Bad
assertTrue("not empty", !r.isEmpty());
Good
assertFalse("not empty", r.isEmpty());
Rule. Don't catch unexpected exceptions.
Bad
public void testCalculation() { try { deepThought.calculate(); assertEquals("Calculation wrong", 42, deepThought.getResult()); } catch(CalculationException ex) { Log.error("Calculation caused exception", ex); } }
Good
public void testCalculation() throw CalculationException { deepThought.calculate(); assertEquals("Calculation wrong", 42, deepThought.getResult()); }
Rule. Avoid too complex test. One test should not contain too many asserts. Consider breaking the test scenario into multiple, shorter test scenarios.
Rule. Avoid external dependencies. Others can simply select one test and then run it without extra configurations. e.g. Data file should be placed in the svn with test code, so that newcomers don't need to manually prepare it.
Rule. Don't us random. That makes test uncertain, hard to debug and maintain. Instead of it, prepare enough special test data to cover boundary conditions.
Rule. Name tests properly. "testLoadFileFromServer" is better than "test".
Rule. Clean up in @After/@AfterClass method to avoid affecting other testcases.