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
2271
votes
35 answers

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

How can I use JUnit 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(); …
SCdF
  • 57,260
  • 24
  • 77
  • 113
1430
votes
23 answers

What is the use of "assert" in Python?

What does assert mean? How is it used?
Hossein
  • 40,161
  • 57
  • 141
  • 175
957
votes
25 answers

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

How do I use Assert (or other Test class) to verify that an exception has been thrown when using MSTest/Microsoft.VisualStudio.TestTools.UnitTesting?
Alex
  • 75,813
  • 86
  • 255
  • 348
687
votes
31 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
  • 16,560
  • 16
  • 61
  • 78
590
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 less…
meade
  • 22,875
  • 12
  • 32
  • 36
485
votes
20 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
  • 81,433
  • 63
  • 146
  • 187
461
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
  • 11,557
  • 7
  • 56
  • 103
447
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
  • 9,149
  • 4
  • 32
  • 38
331
votes
17 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
  • 6,475
  • 8
  • 31
  • 37
303
votes
32 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
  • 3,509
  • 2
  • 17
  • 8
302
votes
8 answers

pytest: assert almost equal

How to do assert almost equal with pytest for floats without resorting to something like: assert x - 0.00001 <= y <= x + 0.00001 More specifically it will be useful to know a neat solution for quickly comparing pairs of float, without unpacking…
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
281
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,514
  • 7
  • 30
  • 42
265
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
  • 185,044
  • 174
  • 569
  • 824
260
votes
10 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
  • 52,936
  • 62
  • 169
  • 241
250
votes
12 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
  • 222,824
  • 274
  • 634
  • 905
1
2 3
99 100