Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting for all of the possible values of a constant
    primarykey
    data
    text
    <p>I have a constant in my application which determines the operation of some other parts of the program. I change this constant to quickly and easily alter the operation of the program.</p> <p>In my case, the constant is a <code>bool</code> so it can have one of two values.</p> <p>I want to write a test that will make sure my code is working whether the constant is set to true or not.</p> <p>For example, say my method is this:</p> <pre><code>public boolean IsEqual(float a, float b) { var epsilon = 0.0001; if (Constants.Exact) return (Math.Abs(a-b) &lt; epsilon); else return (Math.Floor(a) == Math.Floor(b)); } </code></pre> <p><code>Constants</code> looks like this:</p> <pre><code>public static class Constants { /// &lt;summary&gt; /// Determines whether an exact comparison should be made, or whether fractional parts should be disregarded. /// &lt;/summary&gt; public const bool Exact = true; } </code></pre> <p>And the test method is:</p> <pre><code>[TestMethod] public void TestEquality() { var five = 5; var three = 3; Assert.True(Equals(five, three)); } </code></pre> <p>Solutions I could come up with:</p> <ul> <li>Write the test as if the constant doesn't exist, set the constant to <code>true</code> and run tests, then set it to false and run tests. Bad because if I have 8 constants like this, I don't want to run the tests 256 times.</li> <li>Don't make it a constant. Inside the test method, first set constant to true, assert, then false, assert again. However, the reason I made it a constant in the first place is so that it's guaranteed not to change at run-time.</li> </ul> <p>I guess what I really want is a way to make it constant as far as the application proper is concerned, but not a constant as far as the test project is concerned.</p> <p>So how can I make a situation like this work?</p>
    singulars
    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