Questions tagged [assertions]

Assertions enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

48 questions
530
votes
18 answers

Is it OK to have multiple asserts in a single unit test?

In the comment to this great post, Roy Osherove mentioned the OAPT project that is designed to run each assert in a single test. The following is written on the project's home page: Proper unit tests should fail for exactly one reason, that’s…
Restuta
  • 437
  • 3
  • 6
  • 14
75
votes
6 answers

When to use assertions and when to use exceptions?

Often when I write a functions I want to make sure the inputs to it are valid in order to detect such errors as early as possible (I believe these are called preconditions). When a precondition fails, I've always thrown an exception. But I'm…
gablin
  • 17,377
  • 22
  • 89
  • 138
58
votes
3 answers

Are asserts or unit tests more important?

Both asserts and unit tests serve as documentation for a codebase, and a means of discovering bugs. The main differences are that asserts function as sanity checks and see real inputs, whereas unit tests run on specific simulated inputs and are…
dsimcha
  • 17,224
  • 9
  • 64
  • 81
38
votes
9 answers

Is possible to write too many asserts?

I am a big fan of writing assert checks in C++ code as a way to catch cases during development that cannot possibly happen but do happen because of logic bugs in my program. This is a good practice in general. However, I've noticed that some…
Alan Turing
  • 1,533
  • 1
  • 16
  • 20
31
votes
9 answers

"Dead programs tell no lies" in the context of GUI programs

In The Pragmatic Programmer, the authors write: One of the benefits of detecting problems as soon as you can is that you can crash earlier, and crashing is often the best thing you can do. The alternative may be to continue, writing corrupted data…
samfrances
  • 1,065
  • 1
  • 10
  • 15
29
votes
8 answers

Should there be assertions in release builds

The default behavior of assert in C++ is to do nothing in release builds. I presume this is done for performance reasons and maybe to prevent users from seeing nasty error messages. However, I'd argue that those situations where an assert would have…
27
votes
3 answers

Python - assert vs if & return

I am writing a script that does something to a text file (what it does is irrelevant for my question though). So before I do something to the file I want to check if the file exists. I can do this, no problem, but the issue is more that of…
Vader
  • 395
  • 1
  • 3
  • 7
27
votes
3 answers

Should I still use Debug.Assert today?

I recently came across some newly written code that was interspersed with lots of Debug.Assert (C#). Should we still use this widely despite the usage of TDD, BDD and Unit Testing in general?
Dominik Fretz
  • 371
  • 1
  • 3
  • 5
22
votes
9 answers

Are too many assertions code smell?

I've really fallen in love with unit testing and TDD - I am test infected. However, unit testing is normally used for public methods. Sometimes though I do have to test some assumptions-assertions in private methods too, because some of them are…
Flo
  • 1,231
  • 2
  • 12
  • 20
19
votes
8 answers

Why assert for null on a object before asserting on some of its internals?

Let's consider the following test. [Fact] public void MyTest() { // Arrange Code var sut = new SystemWeTest(); // Act Code var response = sut.Request(); // Assert response.Should().NotBeNull(); …
BAmadeusJ
  • 316
  • 2
  • 7
18
votes
1 answer

Unit tests: deferred assertions with Linq

Is it ok to add deferred assertions like this var actualKittens = actualKittens.Select(kitten => { Assert.IsСute(kitten); return kitten }); Why? So I can iterate just once even with statements expecting materialized collection for…
SerG
  • 482
  • 3
  • 12
14
votes
14 answers

Should I raise an exception/error when an optional argument is used but is not necessary?

Take this constructed example: def fetch_person(person_id, country_code=None): if is_fully_qualified(person_id): return person_source_1.fetch_person(person_id) else: return person_source_2.fetch_person(person_id,…
13
votes
5 answers

How can I improve my error checking and handling?

Lately I have been struggling to understand what the right amount of checking is and what the proper methods are. I have a few questions regarding this: What is the proper way to check for errors (bad input, bad states, etc)? Is it better to…
Anon
  • 325
  • 1
  • 3
  • 9
11
votes
1 answer

Duck typing, data validation and assertive programming in Python

About duck typing: Duck typing is aided by habitually not testing for the type of arguments in method and function bodies, relying on documentation, clear code and testing to ensure correct use. About argument validation (EAFP: Easier to ask for…
warvariuc
  • 352
  • 3
  • 12
10
votes
2 answers

code contracts/asserts: what with duplicate checks?

I'm a huge fan of writing asserts, contracts or whatever type of checks available in the language I'm using. One thing that bothers me a bit is that I'm not sure what the common practice is for dealing with duplicate checks. Example situation: I…
stijn
  • 4,108
  • 1
  • 25
  • 31
1
2 3 4