Coding Bootcamp: Unit testing

Definition - Testing

Dynamic verification that the program has the
expected behevior
in a suitably selected final set of test cases

Levels of testing

Testing by goal

Useful testing techniques

Testing practices

Testing activities

Definition - Unit testing

Unit testing is a method of testing individual units of source code to determine if they are fit for use

Unit testing

xUnit frameworks

The first example - JUnit

import org.junit.Assert;
import org.junit.Test;
...
    @Test
    public void testCalculateNoc() {
        RegexBasedMetricsComputer metricsComp = new RegexBasedMetricsComputer("src/designPatterns/RegexBasedMetricsComputer.java");
        Assert.assertEquals(1, metricsComp.calculateNoc());
    }
...

Unit-test plan

Unit testing practices

Annotations - JUnit

Annotations are used over the methods in the Test classes and suites in order to depict their functionality.

Assert

Testing of assumptions is done inside each test method by the use of assertions from the Assert class.

The assert methods compare the actual value returned by a test to the expected value, throwing an AssertionException if the comparison fails.

Fixtures

Exception testing

@Test (expected = NoSuchFileException.class)
    public void testCalculateNocException() throws NoSuchFileException {
        RegexBasedMetricsComputer metricsComp = new RegexBasedMetricsComputer("somepath/somefile.java");
        Assert.assertEquals(1, metricsComp.calculateNoc());
    }

Test suites

Suites allow grouping of Test classes. They are empty classes, annotated with the @RunWith and the @Suite.SuiteClasses annotations

When the suite class run all the tests in the included classes will also run

Exercise 1

Exercise 2


Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.