Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert multiple conditions in a single test, or split into multiple tests? [duplicate]

If you were testing a count function like the one below, is it considered to be 'right' or 'wrong' to test multiple things for the function in one function vs having a test function for each of the tests?

function testGetKeywordCount() {
    $tester = $this -> getDatabaseTester($this -> CompleteDataFile);
    $tester -> onSetUp();

    $KeywordID = 0;
    $this -> setExpectedException('InvalidArgumentException');
    $this -> keyword -> getKeywordCount($KeywordID,'Active');

    $KeywordID = 1;
    $this -> setExpectedException('InvalidArgumentException');
    $this -> keyword -> getKeywordCount($KeywordID,'InvalidStatus');

    $this -> assertEquals(1, $this -> keyword -> getKeywordCount($KeywordID,'Active'));

    $tester -> onTearDown();
}
like image 660
GeoffreyF67 Avatar asked Apr 17 '09 21:04

GeoffreyF67


People also ask

Can I have multiple asserts in one test?

you can have multiple asserts on the same object. they will usually be the same concept being tested.

Can you have multiple assertion in a Junit test?

As long as you clearly label assertions there is no problem in using multiple.

Should you have one assert per test?

“One assertion per test” is a wise rule to keep in mind, because it helps you have tests that fail for a specific reason, and drives you to focus on a specific behavior at a time.


1 Answers

You should have multiple test functions, where each tests its own condition. That way it is easier to spot a failure without debugging.

like image 136
grover Avatar answered Oct 19 '22 23:10

grover