Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Basics

Testing Basics

Avatar for Seth Clark

Seth Clark

April 11, 2017
Tweet

More Decks by Seth Clark

Other Decks in Technology

Transcript

  1. UNIT TESTING ▸ Tests that verify that a specific (small)

    section of code operates as expected. ▸ Often performed on individual methods.
  2. BENEFITS OF UNIT TESTING ▸ Easily cover all combinations of

    inputs and outputs ▸ Can Should be automated ▸ Quick to create ▸ Refactor protection ▸ Documentation PITFALLS OF UNIT TESTING ▸ Testing too much ▸ Designing code that can be unit tested.
  3. BENEFITS OF INTEGRATION TESTING ▸ Covers most use-cases of a

    set of functionality ▸ Can be automated PITFALLS OF INTEGRATION TESTING ▸ Testing too much ▸ Designing code that can be integration tested.
  4. BENEFITS OF SYSTEM TESTING ▸ Can be used to verify

    more complex real customer use- cases PITFALLS OF SYSTEM TESTING ▸ Difficult to write
  5. BENEFITS OF MANUAL TESTING ▸ Can cover tests that require

    interactions with entities outside of your system. PITFALLS OF MANUAL TESTING
  6. JUNIT - TEST CLASS STRUCTURE @BeforeClass public static void setupClass()

    {} @Before public void setup() {} @Test public void testMethod1() {} @Test public void testMethod2() {} @After public void tearDown() {} @AfterClass public static void tearDownClass() {}
  7. JUNIT - ASSERTIONS assertNotNull(new Object()); assertNull(null); assertEquals("String", "String"); assertFalse(false); assertTrue(true);

    assertThat(Arrays.asList(1, 2, 3), hasItems(2, 3)); assertThat("JUnit", both(containsString("J")).and(containsString("t")));
  8. JUNIT - RUNNERS / SUITES @RunWith(Junit4.class) public class Tests {

    @RunWith(Suite.class) @Suite.SuiteClasses({ LoginTests1.class, LoginTests2.class, LoginTests3.class }) public class LoginTestSuite {}
  9. JUNIT - RULES @Rule ExpectedException expectedException = ExpectedException.none(); @Test public

    void throwNPE() { expectedException.expect( NullPointerException.class); throw new NullPointerException(); } @Rule TestRule timeout = Timeout.seconds(1); @Test public void nextEndingTest() { while (true) {} }
  10. TIPS //Use AssertJ or some other assertions library assertThat(new Object()).isNotNull();

    assertThat(true).isTrue(); assertThat(Arrays.asList(1, 2, 3)) .hasSize(3) .contains(2, 3) .doesNotContain(4);
  11. TIPS ▸ Keep your tests organized ▸ Never* ignore failing

    tests ▸ But if you do use @Ignore instead of commenting out the test ▸ Don’t prefix test methods with ‘test’ ▸ Standardize test method names ▸ Test expectations — not implementations