Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A unit test is another piece of software that you write which exercises your main code for acceptance of desired functionality.</p> <p>I could write a calculator program which looks nice, has the buttons, looks like a TI-whatever calculator, and it could produce 2+2=5. Looks nice, but rather than send each iteration of some code to a human tester, with a long list of checks, I, the developer can run some automated, coded, unit tests on my code.</p> <p>Basically, a unit test should be tested itself, by peers, or other careful review to answer "is this testing what I want it to?"</p> <p>The unit test will have a set of "Givens", or "Inputs", and compare these to expected "Outputs".</p> <p>There are, of course, different methodologies on how, when, and how much to use unit tests (check SO for some questions along these lines). However, in their most basic case, they are a program, or a loadable module of some other program, which makes <strong>assertions</strong>.</p> <p>A standard grammar for a unit test might be to have a line of code which looks like this: <code>Assert.AreEqual( a, b )</code>.</p> <p>The unit test method body might set up the inputs, and an actual output, and compare it to the expected output.</p> <pre><code>HelloWorldExample helloWorld = new HelloWorldExample(); string expected = "Hello World!"; string actual = helloWorld.GetString(); Assert.AreEqual( expected, actual ); </code></pre> <p>If your unit test is written in the language of a particular framework (<em>e.g.</em> jUnit, NUnit, etc. ), the results of each method which is marked as part of a "test run" will be aggregated into a set of test results, such as a pretty graph of red dots for failure and green dots for successes, and/or an XML file, etc.</p> <p>In response to your latest comments, "Theory" can provide some real world insight. TDD, Test Driven Development, says a lot about when and how often to use tests. On my latest project, we didn't adhere to TDD, but we sure used unit tests to verify that our code did what it was supposed to do.</p> <p>Say you've chosen to implement the Car interface. The Car interface looks like this:</p> <pre><code>interface ICar { public void Accelerate( int delta ); public void Decelerate( int delta ); public int GetCurrentSpeed(); } </code></pre> <p>You choose to implement the Car interface in the class FordTaurus:</p> <pre><code>class FordTaurus : ICar { private int mySpeed; public Accelerate( int delta ) { mySpeed += delta; } public Decelerate( int delta ) { mySpeed += delta; } public int GetCurrentSpeed() { return mySpeed; } } </code></pre> <p>You're assuming that to decelerate a FordTaurus, one must pass a negative value. However, suppose that you have a set of unit tests written against the Car interface, and they look like this:</p> <pre><code>public static void TestAcceleration( ICar car ) { int oldSpeed = car.GetCurrentSpeed(); car.Accelerate( 5 ); int newSpeed = car.GetCurrentSpeed(); Assert.IsTrue( newSpeed &gt; oldSpeed ); } public static void TestDeceleration( ICar car ) { int oldSpeed = car.GetCurrentSpeed(); car.Decelerate( 5 ); int newSpeed = car.GetCurrentSpeed(); Assert.IsTrue( newSpeed &lt; oldSpeed ); } </code></pre> <p>The test tells you that maybe you've implemented the car interface incorrectly.</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.
    1. 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