Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing with generated inputs and expected outputs
    primarykey
    data
    text
    <p>I'm writing some simple unit tests for a method which acts like a database query: it returns a list of results that can be customised using a limit (max of n results) and a page (the p'th set of n results). I'd like to test a few cases, such as:</p> <ul> <li>Return all the results</li> <li>Return the first page of a limited result set</li> <li>Return an aribtrary page of a limited result set</li> <li>Return the last page of a limited result set which overruns the total number of results</li> <li>Return a page beyond the total number of results</li> </ul> <p>I need to mock the results (effectively names) that are returned, so I'm just generating them with a counter to make a list of "Name #1", "Name #2", etc. for as many as I need. Each test looks (roughly) like:</p> <pre><code>public void testGetMockCandidatesLimited() throws Exception { int numResults = 4; setupMock(numResults); results = queryFunction(...); // Check that the expected number of results was returned assertEquals(numResults, results.size()); // Check that the results are correct and in order for (int i = 0; i &lt; numResults; i++) { assertEquals(result.get(i).getName(), "Name #" + (i + 1)); } } </code></pre> <p>My question is: is it okay to 'generate' the expected answers in this way? This is a trivial example, but the next step was to write a test to write a test to get the second page of two results each, and I had:</p> <pre><code> final int TEST_LIMIT = 2, TEST_PAGE = 1; // Check that the expected number of results was returned assertEquals(numResults, results.size()); // Check that the results are correct and in order for (int i = TEST_LIMIT * TEST_PAGE; i &lt; TEST_LIMIT * TEST_PAGE + TEST_LIMIT; i++) { assertEquals(result.get(i).getName(), "Name #" + (i + 1)); } </code></pre> <p>Now it just so happens that this test is 'correct' in the sense that i will take on the values that I expect it to and ensure that the results are "Name #3" and "Name #4". However it also happens that my queryFunction calculates which results to return in the same way (<code>limit * page + limit</code> kind of thing).</p> <p>This is concerning because if the test generates the expected answers in the same way that the unit under test does, it won't detect if there's a bug in that approach. However naming the input values with constants makes the tests much more readable than just have magic integers between 1 and 4 plugged in everywhere.</p> <p>What is the best way to deal with this situation, for both readability and maintainability (e.g. changing values in the future, should you need to)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload