Jump to content

Self-testing code

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Doc1569 (talk | contribs) at 15:49, 23 March 2022 (Here ive added an example of the jupiter api, where i added some clarity to the assert equals term that is used in mant tests). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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) {
    test();
}

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) {
    test();
    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 varibale is equal to the value given.

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

    }

See also

Further reading

Self-testing code explained by Martin Fowler