Unit testing : 2 scenarios in one test?

google_tech_lead :

I want to know if I have a method to test :

public boolean validString(String str) {
   ...
}

Is is good to do a unit test like this :

public void should_valid_string() {
  assertTrue(validString("okString"));
  assertFalse(validString("wrongString"));
}

or it's better to do two tests ?

Jens Scharmann :

In my opinion this depends on how likely are your test cases fail all at once. In your example, if the first assert fails you don't know if the second one is ok. If you seperate them in two tests you get an atomic answer.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400731&siteId=1