Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to test when writing Unit Tests?

I want to begin unit testing our application, because I believe that this is the first step to developing a good relationship with testing and will allow me to branch into other forms of testing, most interesting BDD with Cucumber.

We currently generate all of our Base classes using Codesmith which are based entirely on the tables in a database. I am curious as to the benefits of generating test cases with these Base classes? Is this poor testing practices?

This leads me to the ultimate question of my post. What do we test when using Unit Tests?

Do we test the examples we know we want out? or do we test the examples we do not want?

Their can be methods that have multiple ways of Failing and multiple ways of Success, how do we know when to stop?

Take a Summing function for example. Give it 1,2 and expect 3 in the only unit test.. how do we know that 5,6 isn't coming back 35?

Question Recap

  • Generating unit tests (Good/Bad)
  • What/How much do we test?
like image 517
James Armstead Avatar asked Mar 10 '10 15:03

James Armstead


4 Answers

Start with your requirements and write tests that test the expected behavior. From that point on, how many other scenarios you test can be driven by your schedule, or maybe by your recognizing non-success scenarios that are particularly high-risk.

You might consider writing non-success tests only in response to defects you (or your users) discover (the idea being that you write a test that tests the defect fix before you actually fix the defect, so that your test will fail if that defect is re-introduced into your code in future development).

like image 167
lance Avatar answered Sep 25 '22 02:09

lance


The point of unit tests is to give you confidence (but only in special cases does it give you certainty) that the actual behavior of your public methods matches the expected behavior. Thus, if you have a class Adder

class Adder { public int Add(int x, int y) { return x + y; } }

and a corresponding unit test

[Test]
public void Add_returns_that_one_plus_two_is_three() {
    Adder a = new Adder();
    int result = a.Add(1, 2);
    Assert.AreEqual(3, result);
}

then this gives you some (but not 100%) confidence that the method under test is behaving appropriately. It also gives you some defense against breaking the code upon refactoring.

What do we test when using Unit Tests?

The actual behavior of your public methods against the expected (or specified) behavior.

Do we test the examples we know we want out?

Yes, one way to gain confidence in the correctness of your method is to take some input with known expected output, execute the public method on the input and compare the acutal output to the expected output.

like image 30
jason Avatar answered Sep 25 '22 02:09

jason


What to test: Everything that has ever gone wrong.

When you find a bug, write a test for the buggy behavior before you fix the code. Then, when the code is working correctly, the test will pass, and you'll have another test in your arsenal.

like image 44
Andy Lester Avatar answered Sep 23 '22 02:09

Andy Lester


1) To start, i'd recommend you to test your app's core logic.

2) Then, use code coverage tool in vs to see whether all of your code is used in tests(all branches of if-else, case conditions are invoked). This is some sort of an answer to your question about testing 1+2 = 3, 5 + 6 = 35: when code is covered, you can feel safe with further experiments.

3)It's a good practice to cover 80-90% of code: the rest of work is usually unefficient: getters-setters, 1-line exception handling, etc.

4) Learn about separation of concerns.

5) Generation unit tests - try it, you'll see, that you can save a pretty lines of code writing them manually. I prefer generating the file with vs, then write the rest TestMethods by myself.

like image 20
Ilya Smagin Avatar answered Sep 23 '22 02:09

Ilya Smagin