Difference between revisions of "QA/test automation rules"
(→Basic rules for JUnit 4) |
|||
Line 81: | Line 81: | ||
'''Rule.''' Avoid too complex test. One test should not contain too many asserts. Consider breaking the test scenario into multiple, shorter test scenarios. <br /> | '''Rule.''' Avoid too complex test. One test should not contain too many asserts. Consider breaking the test scenario into multiple, shorter test scenarios. <br /> | ||
'''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.<br /> | '''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.<br /> | ||
+ | ''Bad'' | ||
+ | <source lang="java"> | ||
+ | public void testFile() { | ||
+ | String testFile = "C:\\test\\test.odt"; | ||
+ | //Do test against on testFile | ||
+ | } | ||
+ | </source> | ||
+ | ''Good'' | ||
+ | <source lang="java"> | ||
+ | public void testFile() { | ||
+ | // test.odt is placed with test code together. Given a relative path, prepareData will return the file's real absolute path. | ||
+ | String testFile = prepareData("test.odt"); | ||
+ | } | ||
+ | </source> | ||
+ | '''Rule.''' Make test method indepdent, not affect or be affected by others. Test methods can be executed in any particular order. So do necessary setup/teardown in @Before/@BeforeClass/@After/@AfterClass method to avoid side effects.<br /> | ||
'''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.<br /> | '''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.<br /> | ||
'''Rule.''' Name tests properly. "testLoadFileFromServer" is better than "test".<br /> | '''Rule.''' Name tests properly. "testLoadFileFromServer" is better than "test".<br /> | ||
− |
Revision as of 03:28, 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.
Bad
public void testFile() { String testFile = "C:\\test\\test.odt"; //Do test against on testFile }
Good
public void testFile() { // test.odt is placed with test code together. Given a relative path, prepareData will return the file's real absolute path. String testFile = prepareData("test.odt"); }
Rule. Make test method indepdent, not affect or be affected by others. Test methods can be executed in any particular order. So do necessary setup/teardown in @Before/@BeforeClass/@After/@AfterClass method to avoid side effects.
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".