On Sun, 01 Nov 2009 08:07:37 0600, Peng Yu wrote:
> It is
> reasonable to assume the log of the number of states of a function or a
> class is proportional to it length. Hence, when a function or a
> class is long, you will never be able to test all its states.
def f(n):
return n + 5
def g(n):
x = n + 1
x += 1
x += 1
x += 1
return x + 1
Function g() has five times the length (in lines) as f(). Does it require
five times the testing? Obviously not. In fact, there's no easy way to
test the internal state of the function from outside, because x is local
to g(). You can't test the value of x from outside, because x is not
exposed to the outside.
The complexity of testing functions is roughly proportional to the number
of paths through the function, not the number of lines. Both f() and g()
have a single path through the function and therefore require the same
amount of testing.
The simplest estimate of the number of paths in a function is the number
of if...else statements. Each pair *doubles* the number of paths:
def func():
if cond1: A
else: B
if cond2: C
else: D
if cond3: E
else: F
There are 2**3 paths that need testing:
ACE ACF ADE ADF BCE BCF BDE BDF
Or consider this example:
def func(x, cond):
for f in [A, B, C, D, E, F]:
if cond(x): x = f(x)
This function has two lines only, but clearly there could be as many as
128 paths that need testing. Ideally your test data should visit each one
of these paths.

Steven
