views:

55

answers:

4

Consider the following code (from a requirement that says that 3 is special for some reason):

bool IsSpecial(int value)
   if (value == 3)
      return true
   else
      return false

I would unit test this with a couple of functions - one called TEST(3IsSpecial) that asserts that when passed 3 the function returns true and another that passes some random value other than 3 and asserts that the function returns false.

When the requirement changes and say it now becomes 3 and 20 are special, I would write another test that verifies that when called with 20 this function returns true as well. That test would fail and I would then go and update the if condition in the function.

Now, what if there are people on my team who do not believe in unit testing and they make this change. They will directly go and change the code and since my second unit test might not test for 20 (it could be randomly picking an int or have some other int hardcoded). Now my tests aren't in sync with the code. How do I ensure that when they change the code some unit test or the other fails?

I could be doing something grossly wrong here so any other techniques to get around this are also welcome.

+1  A: 

Unfortunately, that specific scenario is something that is difficult to guard against. With a function like IsSpecial, it's unrealistic to test all four billion negative test cases, so, no, you're not doing something grossly wrong.

Here's what comes to me off the top of my head. Many repositories have hooks that allow you to run some process on each check-in, such as running the unit tests. It's possible to set a criterion that newly checked in code must reach some threshold of code coverage under unit tests. If the commit does not meet certain metrics, it is rejected.

I've never had to set one of these systems up, so I don't know what is involved, but I do know it's possible.

And believe me, I feel your pain. I work with people who are similarly resistant to unit testing.

haydenmuhl
+1  A: 

That's a good question. As you note a Not3IsNotSpecial test picking a random non-3 value would be the traditional approach. This wouldn't catch a change in the definition of "special".

In a .NET environment you can use the new code contracts capability to write the test predicate (the postcondition) directly in the method. The static analyzer would catch the defect you proposed. For example:

Contract.Ensures(value != 3 && Contract.Result<Boolean>() == false);

I think anybody that's a TDD fan is experimenting with contracts now to see use patterns. The idea that you have tools to prove correctness is very powerful. You can even specify these predicates for an interface.

The only testing approach I've seen that would address this is Model Based Testing. The idea is similar to the contracts approach. You set up the Not3IsNotSpecial condition abstractly (e.g., IsSpecial(x => x != 3) == false)) and let a model execution environment generate concrete tests. I'm not sure but I think these environments do static analysis as well. Anyway, you let the model execution environment run continuously against your SUT. I've never used such an environment, but the concept is interesting.

Rob
+1  A: 

One thing you need to think about is why 3 is a special character and others are not. If it is defining some aspect of your application, you can take that aspect out and make an enum out of it.

Now you can check here that this test should fail if value doesn't exist in enum. And for enum class write a test to check for possible values. If there is new possible value being added your test should fail.

So your method will become:

bool IsSpecial(int value)
   if (SpecialValues.has(value))
      return true
   else
      return false

and your SpecialValues will be an enum like:

enum SpecialValues {

Three(3), Twenty(20)

   public int value;
}

and now you should write to test possible values for enum. A simple test can be to check total number of possible values and another test can be to check the possible values itself

Ankit
if I'm in C++ then there is no way to do enum SpecialValues and check SpecialValues.has(3) or SpecialValues.has(20).That becomes the problem then right?
obelix
I am not sure about this but can you loop over the values in C++. If yes than you can implement has functionality inside your IsSpecial.
Ankit
A: 

The other point to make is that in a less contrived example:

  • 20 might have been some valid condition to test for based on knowledge of the business domain. Writing tests in a BDD style based on knowledge of the business problem might have helped you explicitly catch it.

  • 4 might have been a good value to test for due to its status as a boundary condition. This may have been more likely to change in the real world so would more likely show up in a full test case.