asserting in code vs unit tests

  • Thread starter Esad Hajdarevic
  • Start date
E

Esad Hajdarevic

Hi!

I talked to few fellow ruby programmers, and most of them agreed that
unit tests are the place where the asserts should be placed, however in
my own opinion, unit tests mostly never cover everything and asserts in
development versions ($DEBUG) are very handy. What is your opinion?

Esad
 
G

Gareth Reeves

Esad said:
Hi!

I talked to few fellow ruby programmers, and most of them agreed that
unit tests are the place where the asserts should be placed, however in
my own opinion, unit tests mostly never cover everything and asserts in
development versions ($DEBUG) are very handy. What is your opinion?

Esad

Personally, I like the asserts in the test case.

It doesnt clutter the code, you can test each circumstance and it helps
with design because you have to treat the code as a unit with inputs and
outputs.

Gareth
 
M

Meinrad Recheis

------=_Part_16624_9458888.1141597165291
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi!

I talked to few fellow ruby programmers, and most of them agreed that
unit tests are the place where the asserts should be placed, however in
my own opinion, unit tests mostly never cover everything and asserts in
development versions ($DEBUG) are very handy. What is your opinion?

Esad
asserts in code are good to ensure preconditions and postconditions of
methods. this is "design by contract".
asserts in unittests can often only be used to check postconditions of the
tested methods.

i prefer precondition asserts in debug code that get filtered out by a
preprocessor in the release version (hence no performance cost).
i often have the assertions followed by errorhandling code that tries to
handle the same issues gracefully in release code.

-- henon

------=_Part_16624_9458888.1141597165291--
 
E

E. Saynatkari

Esad said:
Hi!

I talked to few fellow ruby programmers, and most of them agreed that
unit tests are the place where the asserts should be placed, however in
my own opinion, unit tests mostly never cover everything and asserts in
development versions ($DEBUG) are very handy. What is your opinion?

If your unit tests do not cover everything, how can
any other kinds? :)


E
 
C

chiaro scuro

Esad Hajdarevic wrote:
If your unit tests do not cover everything, how can
any other kinds? :)

These are two different issues.

Your asserts/contracts should check that the methods are being passed
what they expect to be passed to them. Postconditions will probably
check some invariants on the results of the methods.

Your test should not test what the contracts already check. The tests
should check that if you pass correct values you get the expected
results.

Eg: assume that you have a method that takes only positive numbers.
you will have a contract/assertion checking that you don't pass any
negatives and a test suite that checks that, given a set of positive
inputs, you get the correct outputs.
 
E

E. Saynatkari

Chiaro said:
These are two different issues.

Your asserts/contracts should check that the methods are being passed
what they expect to be passed to them. Postconditions will probably
check some invariants on the results of the methods.

Yes, I agree, and this was partly the point I was making.
There are situations where functional tests are required
and there are some things that just require real-time
constraints (any non-predetermined input, usually).
Your test should not test what the contracts already check. The tests
should check that if you pass correct values you get the expected
results.

Eg: assume that you have a method that takes only positive numbers.
you will have a contract/assertion checking that you don't pass any
negatives and a test suite that checks that, given a set of positive
inputs, you get the correct outputs.

Or, instead, you could in many circumstances ascertain that the
method that produces the input does not produce negative numbers;
this is something that a unit test would do and it would eliminate
the need of a constraint :)


E
 
C

chiaro scuro

Or, instead, you could in many circumstances ascertain that the
method that produces the input does not produce negative numbers;
this is something that a unit test would do and it would eliminate
the need of a constraint :)

We are on something interesting here. I am tempted to agree with you,
but for the sake of discussion and to explore the idea with you I will
say:

* a test doesn't prove that it will never produce a negative number,
it simply checks that it doesn't do it under certain very specific
conditions
* a postcondition will always check that it doesn't produce a
negative number and it will warn you if it does.

In this sense pre and post conditions seems to serve different purposes.

the precondition defines a scope of valid input and checks that that
input is well formed.
tests check a method, passing it valid input and expecting a 'specific' res=
ult.
a postcondition defines a scope of valid output and checks that the
output is well formed.

pre/post alone are not enough because they just check the domain of variabl=
es.

you need tests to verify that at least under some conditions the
method yields meaningful results.

tests alone however are never exhaustive and cannot prove correctness.
assertions can catch some types of 'wrongness'.
 
P

Pat Maddox

We are on something interesting here. I am tempted to agree with you,
but for the sake of discussion and to explore the idea with you I will
say:

* a test doesn't prove that it will never produce a negative number,
it simply checks that it doesn't do it under certain very specific
conditions
* a postcondition will always check that it doesn't produce a
negative number and it will warn you if it does.

In this sense pre and post conditions seems to serve different purposes.

the precondition defines a scope of valid input and checks that that
input is well formed.
tests check a method, passing it valid input and expecting a 'specific' r= esult.
a postcondition defines a scope of valid output and checks that the
output is well formed.

pre/post alone are not enough because they just check the domain of varia= bles.

you need tests to verify that at least under some conditions the
method yields meaningful results.

tests alone however are never exhaustive and cannot prove correctness.
assertions can catch some types of 'wrongness'.

Isn't that what exceptions are for?

def foo(a)
raise 'Argument can not be negative' if a < 0

b =3D something_that_should_always_be_positive

raise 'Something strange happened and b was negative' if b < 0
b
end

foo expects a to be positive, so when a negative number comes in
that's an exceptional case. It also should always return a positive
number, so if b is negative then something really went wrong and it
should terminate.

Pat
 
C

chiaro scuro

I would get my assertions to raise exceptions too. They are just a
specific kind of exception, related to the domain of inputs and
outputs, different from exceptions generated from something that goes
wrong in the middle.

What do other people think about the relationship between assertions
and exceptions?
 
E

E. Saynatkari

Chiaro said:
We are on something interesting here. I am tempted to agree with you,
but for the sake of discussion and to explore the idea with you I will
say:

* a test doesn't prove that it will never produce a negative number,
it simply checks that it doesn't do it under certain very specific
conditions
* a postcondition will always check that it doesn't produce a
negative number and it will warn you if it does.

In this sense pre and post conditions seems to serve different purposes.

the precondition defines a scope of valid input and checks that that
input is well formed.
tests check a method, passing it valid input and expecting a 'specific'
result.
a postcondition defines a scope of valid output and checks that the
output is well formed.

pre/post alone are not enough because they just check the domain of
variables.

you need tests to verify that at least under some conditions the
method yields meaningful results.

tests alone however are never exhaustive and cannot prove correctness.
assertions can catch some types of 'wrongness'.

Sure, but you could also just have a stick of bad RAM :)

The goal is to be reasonably certain that the program behaves
as defined by the tests. This is aided by the granular and
gradual evolution of the tests. You can never really be sure
of anything.


E
 
J

John Carter

Unit Tests and DbC assertions are complementary, not exclusive. I always
do both.

I tend to put the "post condition" assertions in the Unit Tests and
invariant and pre-condition assertions in the code.

Why?

1) The pre-condition assertions are "tests of the tests". Test's are code
too, therefore have bugs.

2) Post-conditions are effectively the asserts found in the unit test. ie.
If you have strong test coverage, don't bother with post-condition
asserts, refactor them into the unit test code.

2) Client code using my code has bugs and the precondition asserts catch a
lot of them very rapidly and at the right point. A the client code Unit
test postcondition assert says the client (or maybe the lower layer)
code did vaguely the wrong thing somewhere someplace sometime. A
Precondition assert says the client code violated a pre-condition
RIGHT HERE.

3) Invariant code are Object Sanity Checks. Stroustrup (author of C++)
recommends quite sanely that you should only spin a class if and only if
you have an invariant to protect. Invariant checking method is a handy
way of checking at run time that your class is doing what it was
designed to do.

4) I always throw vanilla Exceptions on assertion failure. It meshes
nicely with the stacktrace mechanism of Ruby, but so far I can't think of
any good reason to create a new exception class for them.



I would get my assertions to raise exceptions too. They are just a
specific kind of exception, related to the domain of inputs and
outputs, different from exceptions generated from something that goes
wrong in the middle.

What do other people think about the relationship between assertions
and exceptions?



John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.
 
C

chiaro scuro

Using assertions I could be pretty much sure that certain conditions
would never never ever verify :) I couldn't do that with tests
though.

tests make sure that something happens.
assertions make sure that something doesn't happen.
 
C

chiaro scuro

Interesting. Could you expand on "recommends quite sanely that you
should only spin a class if and only if you have an invariant to
protect"? it feels right but I am not sure I get it completely.

Another thing.. one big problems that I have with assertions is
duplication. Do you assert any method? do you assert on class public
methods? It's very to sprinkle the same assertions all over the place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top