Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Perhaps, but not necessarily.</p> <p>Functional tests can be thought of as "Black Box" tests whereby you are looking at a specific function (regardless if that's a single method, module, system, etc) and checking that for a given input, you get a given output.</p> <p>However, if the functional test fails all you can say is that the system is faulty; it doesn't necessarily give you any indication of <em>what part</em> of the system is to blame. Of course you will go off and diagnose the problem but you don't know up front what the problem is.</p> <p>e.g</p> <pre><code>//assuming you have a UserService that amongst other things passes through to a UserRepository var repo = new UserRepository(); var sut = new UserService(repo); var user = sut.GetUserByID(1); Assert.IsNotNull(user); //Suppose this fails. </code></pre> <p>In the above case, you don't know if it's because the UserService's <code>GetUserByID()</code> function is faulty - perhaps it did not call <code>repo.GetUserByID</code> and just returned null, perhaps it did get a <code>User</code> from <code>repo</code> but accidentally then returned an uninitialized temp variable, etc - or perhaps it's because the dependency (<code>repo</code>) itself is faulty. In any case, you'll have to debug into the issue.</p> <p>Unit tests, on the other hand, are more like "White Box" tests whereby you have effectively taken the cover off the system and are testing <em>how</em> the system behaves in order to do what you want it to do.</p> <p>e.g. (code below may not compile and is just for demo purposes)</p> <pre><code>//setup a fake repo and fake the GetUserByID method to return a known instance: var repo = new Mock&lt;UserRepository&gt;(); var user = new User{ID=1;} repo.Setup(r=&gt;r.GetUserByID(1).Returns(user); var sut = new UserService(repo.Object); var actual= sut.GetUserByID(1); //now make sure we got back what we expected. If we didn't then UserService has a problem. Assert.IsNotNull(user); Assert.AreEqual(user,actual); </code></pre> <p>In this case you are explicitly verifying that <code>sut.GetUserByID()</code> behaves in the expected way - that it invokes the <code>repo.GetUserByID()</code> and returns resultant object, without mangling it or changing it.</p> <p>If this unit test passes, but the functional test fails, you can say with confidence that the problem does not lie with the <code>UserService</code>, but with the <code>UserRepository</code> class, without even looking at the code. That may or may not save you time, it really depends on how complex your functional units are.</p>
 

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