Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Personally I'd <em>consider</em> using a single test for all of the parameters. That doesn't follow the normal dogma of unit testing, but it increases the readability of the tests (by minimizing the amount of test code which is dedicated to a pretty repetitive case) and doesn't have much in the way of downsides. Yes, if the test fails you don't know whether all of the checks after the first failing one will also fail - but is that <em>really</em> a problem in practice?</p> <p>The important point is to make sure that you've got a short cut for testing the case. For instance, you might write something like this (if your unit test framework doesn't have it already):</p> <pre><code>public static void ExpectException&lt;T&gt;(Action action) where T : Exception { try { action(); Assert.Fail("Expected exception " + typeof(T).Name); } catch (T exception) { // Expected } } </code></pre> <p>Then you can write:</p> <pre><code>[Test] public void MyMethodFailsWithInvalidArguments() { ExpectException&lt;ArgumentNullException&gt;(() =&gt; myClass.MyMethod(null)); ExpectException&lt;ArgumentException&gt;(() =&gt; myClass.MyMethod("")); } </code></pre> <p>Much more concise than doing each one with an individual try/catch block, or even using an <code>ExpectedException</code> attribute and multiple tests.</p> <p>You might want overloads for cases where you also want to verify that in each case, no mocked objects have been touched (to check that side-effects are avoided) or possibly overloads for common exceptions like <code>ArgumentNullException</code>.</p> <p>For single-parameter methods you could even write a method to encapsulate exactly what you need:</p> <pre><code>public void ExpectExceptionForNullAndEmptyStrings(Action&lt;string&gt; action) { ExpectException&lt;ArgumentNullException&gt;(() =&gt; action(null)); ExpectException&lt;ArgumentException&gt;(() =&gt; action("")); } </code></pre> <p>then call it with:</p> <pre><code>[Test] public void MyMethodFailsWithInvalidArguments() { // This *might* work without the ExpectExceptionForNullAndEmptyStrings(myClass.MyMethod); } </code></pre> <p>... and maybe another one for methods with a single parameter but a non-void return type.</p> <p>That's possibly going a bit far though :)</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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