Questions tagged [assert]

An assertion is a statement, which aborts a program when it evaluates to false. Assert is typically used for debugging and situations which should never happen.

It is normally bad practice to use asserts in deployed software, because it typically provides information that is useful only to programmers; exceptions are preferred in this case. Also, assertions are not for use in validating input or in other situations where exceptions are preferred.

However, asserts can be used frequently when designing, to make sure that design requirements are met (for example, when designing-by-contract), and in debugging to make sure that code which is incorrect fails as quickly as possible. Assertions often give line numbers and file names, which makes tracking down where code failed easier than with other methods like core dumps.

C and C++ have assert in "assert.h". Most other languages have assert as a built-in (Python, Ruby, Java, and others).

2706 questions
211
votes
21 answers

Is assert evil?

The Go language creators write: Go doesn't provide assertions. They are undeniably convenient, but our experience has been that programmers use them as a crutch to avoid thinking about proper error handling and reporting. Proper error handling…
Frank
  • 64,140
  • 93
  • 237
  • 324
204
votes
15 answers

PHPUnit: assert two arrays are equal, but order of elements not important

What is a good way to assert that two arrays of objects are equal, when the order of the elements in the array is unimportant, or even subject to change?
koen
  • 13,349
  • 10
  • 46
  • 51
193
votes
17 answers

When should assertions stay in production code?

There's a discussion going on over at comp.lang.c++.moderated about whether or not assertions, which in C++ only exist in debug builds by default, should be kept in production code or not. Obviously, each project is unique, so my question here is…
Carl Seleborg
  • 13,125
  • 11
  • 58
  • 70
189
votes
9 answers

C# - What does the Assert() method do? Is it still useful?

I am debugging with breakpoints and I realize the assert call? I thought it was only for unit tests. What does it do more than breakpoint? Since I can breakpoint, why should I use Assert?
Pokus
  • 11,383
  • 11
  • 37
  • 27
169
votes
5 answers

Use NUnit Assert.Throws method or ExpectedException attribute?

I have discovered that these seem to be the two main ways of testing for exceptions: Assert.Throws(()=>MethodThatThrows()); [ExpectedException(typeof(Exception))] Which of these would be best? Does one offer advantages over the other?…
SamuelDavis
  • 3,312
  • 3
  • 17
  • 19
166
votes
4 answers

How to assertThat something is null with Hamcrest?

How would I assertThat something is null? for example assertThat(attr.getValue(), is("")); But I get an error saying that I cannot have null in is(null).
user2811419
  • 1,923
  • 2
  • 14
  • 15
165
votes
11 answers

Add custom messages in assert?

Is there a way to add or edit the message thrown by assert? I'd like to use something like assert(a == b, "A must be equal to B"); Then, the compiler adds line, time and so on... Is it possible?
Killrazor
  • 6,856
  • 15
  • 53
  • 69
149
votes
9 answers

What does static_assert do, and what would you use it for?

Could you give an example where static_assert(...) ('C++11') would solve the problem in hand elegantly? I am familiar with run-time assert(...). When should I prefer static_assert(...) over regular assert(...)? Also, in boost there is something…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
144
votes
6 answers

"assert" statement with or without parentheses

Here are four simple invocations of assert: >>> assert 1==2 Traceback (most recent call last): File "", line 1, in ? AssertionError >>> assert 1==2, "hi" Traceback (most recent call last): File "", line 1, in ? AssertionError:…
new name
  • 15,861
  • 19
  • 68
  • 114
138
votes
8 answers

What does the "assert" keyword do?

What does assert do? For example in the function: private static int charAt(String s, int d) { assert d >= 0 && d <= s.length(); if (d == s.length()) return -1; return s.charAt(d); }
Peiska
  • 1,789
  • 4
  • 17
  • 15
131
votes
14 answers

Design by contract using assertions or exceptions?

When programming by contract a function or method first checks whether its preconditions are fulfilled, before starting to work on its responsibilities, right? The two most prominent ways to do these checks are by assert and by exception. assert…
andreas buykx
  • 12,608
  • 10
  • 62
  • 76
129
votes
9 answers

Why are assertEquals() parameters in the order (expected, actual)?

Why do so many assertEquals() or similar functions take the expected value as the first parameter and the actual one as second? This seems counterintuitive to me, so is there a particular reason for this unusual order?
jor
  • 1,299
  • 2
  • 8
  • 3
125
votes
16 answers

Static assert in C

How can compile-time static asserts be implemented in C (not C++), with particular emphasis on GCC?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
116
votes
6 answers

Disable assertions in Python

How do I disable assertions in Python? That is, if an assertion fails, I don't want it to throw an AssertionError, but to keep going. How do I do that?
Claudiu
  • 224,032
  • 165
  • 485
  • 680
112
votes
5 answers

Is using assert() in C++ bad practice?

I tend to add lots of assertions to my C++ code to make debugging easier without affecting the performance of release builds. Now, assert is a pure C macro designed without C++ mechanisms in mind. C++ on the other hand defines std::logic_error,…
Fabian Knorr
  • 3,134
  • 3
  • 20
  • 32