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).

2407 questions
2100
votes
34 answers

How do you assert that a certain exception is thrown in JUnit 4 tests?

How can I use JUnit4 idiomatically to test that some code throws an exception? While I can certainly do something like this: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean thrown = false; try { foo.doStuff(); } catch…
SCdF
  • 51,261
  • 23
  • 74
  • 108
1115
votes
22 answers

What is the use of "assert" in Python?

I have been reading some source code and in several places I have seen the usage of assert. What does it mean exactly? What is its usage?
Hossein
  • 33,283
  • 53
  • 126
  • 172
871
votes
22 answers

How do I use Assert to verify that an exception has been thrown?

How do I use Assert (or other Test class) to verify that an exception has been thrown?
Alex
  • 71,233
  • 79
  • 245
  • 337
630
votes
28 answers

How to automatically generate a stacktrace when my program crashes

I am working on Linux with the GCC compiler. When my C++ program crashes I would like it to automatically generate a stacktrace. My program is being run by many different users and it also runs on Linux, Windows and Macintosh (all versions are…
KPexEA
  • 15,698
  • 16
  • 57
  • 76
516
votes
15 answers

Best practice for using assert?

Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes? Is assert x >= 0, 'x is less than zero' better or worse than if x < 0: raise Exception, 'x is…
meade
  • 21,435
  • 12
  • 29
  • 36
458
votes
19 answers

How to check if an object is a list or tuple (but not string)?

This is what I normally do in order to ascertain that the input is a list/tuple - but not a str. Because many times I stumbled upon bugs where a function passes a str object by mistake, and the target function does for x in lst assuming that lst is…
Sridhar Ratnakumar
  • 68,948
  • 61
  • 139
  • 172
439
votes
11 answers

Why doesn't JUnit provide assertNotEquals methods?

Does anybody know why JUnit 4 provides assertEquals(foo,bar) but not assertNotEqual(foo,bar) methods? It provides assertNotSame (corresponding to assertSame) and assertFalse (corresponding to assertTrue), so it seems strange that they didn't bother…
Chris B
  • 8,939
  • 4
  • 30
  • 36
376
votes
15 answers

PHPUnit assert that an exception was thrown?

Does anyone know whether there is an assert or something like that which can test whether an exception was thrown in the code being tested?
Felipe
  • 10,031
  • 7
  • 45
  • 97
280
votes
16 answers

What is “assert” in JavaScript?

What does assert mean in JavaScript? I’ve seen something like: assert(function1() && function2() && function3(), "some text"); And would like to know what the method assert() does.
frrlod
  • 5,605
  • 8
  • 26
  • 36
268
votes
9 answers

What is the "assert" function?

I've been studying OpenCV tutorials and came across the assert function; what does it do?
eomer
  • 3,284
  • 5
  • 24
  • 35
260
votes
6 answers

differences between 2 JUnit Assert classes

The JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is? The classes I'm referring to are: junit.framework.Assert and org.junit.Assert.
Dónal
  • 176,670
  • 166
  • 541
  • 787
233
votes
29 answers

How to do a JUnit assert on a message in a logger

I have some code-under-test that calls on a Java logger to report its status. In the JUnit test code, I would like to verify that the correct log entry was made in this logger. Something along the following lines: methodUnderTest(bool x){ if(x) …
Jon
  • 2,729
  • 2
  • 15
  • 8
219
votes
9 answers

How do I check (at runtime) if one class is a subclass of another?

Let's say that I have a class Suit and four subclasses of suit: Heart, Spade, Diamond, Club. class Suit: ... class Heart(Suit): ... class Spade(Suit): ... class Diamond(Suit): ... class Club(Suit): ... I have a method which receives…
snakile
  • 47,358
  • 57
  • 160
  • 231
208
votes
10 answers

AssertContains on strings in jUnit

Is there a nicer way to write in jUnit String x = "foo bar"; Assert.assertTrue(x.contains("foo"));
ripper234
  • 202,011
  • 255
  • 600
  • 878
204
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
  • 58,417
  • 87
  • 227
  • 317
1
2 3
99 100