How to apply a “one statement per test” to a relationship condition OR

He acknowledged that "one statement per trial" in the statement. A statement is written well, as shown below:

Assert((foo != null) && (bar != null)); 

The better chocie:

  Assert(foo != null); Assert(bar != null); 

The question is, if the statement:

  Assert((foo == null) || (foo.length == 0)); 

The ratio of OR, not AND.

Is there a way to make “One statement per test” while maintaining logic?

+4
source share
3 answers

The idea of ​​the manual is that you test only one logical thing (which can come down to saying a few in some cases) in one test. Therefore, if the test fails, you know the exact reason for its failure and can quickly enter a specific block of code. To take an example from this page , if the next test failed, I know that something is wrong with how the country is extracted / determined in the address type,

 public void testCountry() throws Exception { assertEquals("COUNTRY", anAddress.getCountry()); } 

Compare this to a multi-statement test version, it may fail for several reasons (and if you are not using the helpful assert messages), you need to debug the test (yuck!).

I will need to see your full test. From what he sees, it seems that you are checking the collection for something not found in the script. In this case, it is recommended that you return an empty collection so that clients do not check for null. Since your test is also a client, life is also simplified: Assert.AreEqual (0, foo.length)

+4
source

The problem may not be in the statement, but in the Arrange or Act section of the test part: when you execute unit-test, you call the [small] part of the code in a controlled environment . That is, you must know how the software under test will behave, and therefore, know whether it will return a null pointer or an empty string. Each test should have one expected result .

... If you do not use your test against several functions / methods that behave differently, or some third-party code that seems to behave differently in different situations. If so, “one statement for each rule” is just a guideline, and you can use the statement as shown, this is a question. If this test fails, it will mean that the returned foo is a non-empty string.

Or you can create a function to check for empty strings, which will also check to see if a line pointer is specified:

 Assert(is_empty_string(foo)); 

This string class can provide this method.

+4
source

Of course: one test provides a null statement to foo, ensures that a nonzero foo with a length of 0 also fails (and others test cases [s] where foo is not null and has a length! = 0). How does it work than checking AND?

Edit: pseudo code on request ...:

  should_assert(themethod(foo=null)) fakefoo = fakewhatever(length=0) should_assert(themethod(foo=fakefoo)) ...rest of tests w/foo not null, w/length != 0 

for example, in Python unittest with a fixed pseudocode mock , it could be:

  self.assertRaises(AssertionError, theobj.themethod, null) fakefoo = mock.makeObj(length=0) self.assertRaises(AssertionError, theobj.themethod, fakefoo)) ...rest of tests w/foo not null, w/length != 0 
+1
source

Source: https://habr.com/ru/post/1285644/


All Articles