Should I write tests that directly display input to output or dynamically generate a result?

Old manual programmer, new for writing test cases (shame for me I know).

It seems to me that there are two ways to write test cases:

  • Match fixed inputs with fixed outputs and claim that they match
  • Generated cards or fixed inputs for dynamic outputs and claim to match

# Static testing:
assert_equal "test".upcase, "TEST"

# Dynamic testing:
assert_equal person.full_name, "#{person.title} #{person.first_name} #{person.last_name}"

Of course, there are pros and cons to each approach. Duplication of implementation details is erroneous, but allows me to generate data samples and run tests on it. Hard coding values ​​make the correct conclusion very explicit, but do not seem to allow code reuse.

? ? , ?

+3
2

, , check quickcheck, ...

.

, . , , . .

(KAT)

, , . , KAT , (, -), , (: ). decrypt(key,encrypt(key,msg)) == msg.

, - 0 < area(triangle) < triangle.width * triangle.height, . , , , - - , .

, ,

( ):

, concat(xs,ys) = xs ++ ys. ? , , ! ? ! ? !

prop_len(xs,ys) = len(xs) + len(ys) = len(concat(xs,ys))
prop_elem(xs,ys) =
    let cs = concat(xs,ys)
    elem(head xs, cs) && elem(head ys, cs) && prop_elem(tail xs,ys) && prop_elem(xs,tail ys)
// Yes, I left out the error checking for empty list, sue me.

?

+1

, , , , , - .

, , , . . .

, -, . , API. , . , , , dev . , "", .

, , , , .

+1

Source: https://habr.com/ru/post/1786063/


All Articles