Testing a function that always returns true

How to write a test for the next function?

bool IsAnInterger(int ignore) 
{
    return true
}

I don’t have enough time to repeat each integer (for the actual code, the parameter is not even an integer).

This is used as part of the specification template, so I can implement a Null Object.

+3
source share
3 answers

... testing can be a very effective way to show errors, but it is hopelessly inadequate to show errors.

- Edser W. Dijkstra

I would say that it is pointless to try to completely exhaust the black box by checking this function. It is better to test it in a context similar to where it will be used.

+6
source

TDD , . : ? - , , - , , .

:

, , . a Null ? , , . , ( ). . .

+2

, " " . , n.

#include <limits>
#include <cassert>

bool IsAnInteger(int) 
{
    return true;
}

int main()
{
    assert(IsAnInteger(std::numeric_limits<int>::min())); // First
    assert(IsAnInteger(0)); // n
    assert(IsAnInteger(1)); // n+1
}

Hold on!

for actual code, the parameter is not even an integer

What is it?

#include <cassert>

template <class T>
bool IsAnInteger(const T&) 
{
    return true;
}

int main()
{
    assert(IsAnInteger(0)); // First
    assert(IsAnInteger("I am not a number!")); // n
    assert(IsAnInteger(42.0f)); // n+x
}

Your function has 100% test coverage, and your unit tests accurately document how it behaves. In TDD, you just need to write enough code for your unit tests to pass. You are done with this and can move on.

0
source

All Articles