Self-testing code

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Self-testing code is software that incorporates built-in tests (see test-first development).

In Java, to execute a unit test from the command line, a class can have methods like the following.

// Executing <code>main</code> runs the unit test. 
public static void main(String[] args) {

static void test() {
    assert foo == bar;

To invoke a full system test, a class can incorporate a method call.

public static void main(String[] args) {
    TestSuite.test();    // invokes full system test

In addition, Java has some Jupiter API libraries for self-testing code. assert can be used in various ways such as assert equals, which checks if the given variable is equal to the value given.

void checkplayer() {
        Board board = new Board(10);
        int check = board.getCurrentPlayer(1);
        assertEquals(1, check);


See also[edit]

Further reading[edit]

Self-testing code explained by Martin Fowler