Unit tests for ONOS are built using the JUnit framework, version 4.11. Unit tests are used to verify correctness of implementations and are run as part of every full build of ONOS. The nightly SonarQube build runs all of the unit tests and produces coverage data that can be seen here.
How to Write Good Tests
Unit tests should be as short, fast and reliable as they can be. Every developer on the project will be running your tests, they should be easy to run and produce accurate results. Here are some key ways to write tests that are not brittle:
If you add a class, consider adding the following basic tests for it first:
Any class that defines equals(), hashCode(), or toString() must test these methods. The Google Guava EqualsTester class provides full coverage for these methods:
final FlowId flowId1 = FlowId.valueOf(1); final FlowId sameAsFlowId1 = FlowId.valueOf(1); final FlowId flowId2 = FlowId.valueOf(2); /** * Checks the operation of equals(), hashCode() and toString() methods. */ @Test public void testEquals() { new EqualsTester() .addEqualityGroup(flowId1, sameAsFlowId1) .addEqualityGroup(flowId2) .testEquals(); } |
Utility test methods are provided to check that classes are properly defined immutable classes, immutable base classes or utility classes. Using these methods will assure that other developers don't make changes to your classes that violate your assumptions.
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; /** * Checks that the FlowId class is immutable. */ @Test public void testImmutability() { assertThatClassIsImmutable(FlowId.class); } |
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; /** * Checks that the DefaultFlowRule class is immutable but can be inherited * from. */ @Test public void testImmutability() { assertThatClassIsImmutableBaseClass(DefaultFlowRule.class); } |
import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility; /** * Check that the Criteria class is a valid utility class. */ @Test public void testCriteriaUtility() { assertThatClassIsUtility(Criteria.class); } |
A basic test for each one of your constructors and a check for the values returned by data access methods should always be included.
/** * Checks the construction of a FlowId object. */ @Test public void testConstruction() { final long flowIdValue = 7777L; final FlowId flowId = FlowId.valueOf(flowIdValue); assertThat(flowId, is(notNullValue())); assertThat(flowId.value(), is(flowIdValue)); } |
Hamcrest is a powerful framework for defining matchers for data values in tests. ONOS uses version 1.3 of Hamcrest. Contributors are encouraged to learn about and use the Hamcrest framework, particularly when working with arrays and collections. Hamcrest also gives you the ability to write your own matchers, which will make your tests easier to read and easier to extend by other developers.
Here is an example of a custom Hamcrest matcher, that matches a Criterion type in a TrafficSelector object:
/** * Hamcrest matcher to check that a selector contains a * Criterion with the specified type. */ public static final class CriterionExistsMatcher extends TypeSafeMatcher<TrafficSelector> { private final Criterion.Type type; /** * Constructs a matcher for the given criterion type. * * @param typeValue criterion type to match */ public CriterionExistsMatcher(Criterion.Type typeValue) { type = typeValue; } @Override public boolean matchesSafely(TrafficSelector selector) { final Set<Criterion> criteria = selector.criteria(); return notNullValue().matches(criteria) && hasSize(1).matches(criteria) && notNullValue().matches(selector.getCriterion(type)); } @Override public void describeTo(Description description) { description.appendText("a criterion with type \" "). appendText(type.toString()). appendText("\""); } } /** * Creates a criterion type matcher. Returns a matcher * for a criterion with the given type. * * @param type type of Criterion to match * @return Matcher object */ @Factory public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) { return new CriterionExistsMatcher(type); } /** * Tests the builder functions that add specific criteria. */ @Test public void testCriteriaCreation() { TrafficSelector selector; selector = DefaultTrafficSelector.builder() .matchInport(PortNumber.portNumber(11)).build(); assertThat(selector, hasCriterionWithType(Type.IN_PORT)); } |