Assertion is a method of verifying, if the code works as it was designed to. For instance, after reading an XML file, the result should contain exactly one root node. Failed assertion means, that program is in an unstable state and usually in such case its execution is terminated.
Questions tagged [assertions]
735 questions
1590
votes
18 answers
Cycles in family tree software
I am the developer of some family tree software (written in C++ and Qt). I had no problems until one of my customers mailed me a bug report. The problem is that the customer has two children with their own daughter, and, as a result, he can't use my…

Partick Höse
- 11,121
- 3
- 15
- 9
218
votes
8 answers
Comparing arrays in JUnit assertions, concise built-in way?
Is there a concise, built-in way to do equals assertions on two like-typed arrays in JUnit? By default (at least in JUnit 4) it seems to do an instance compare on the array object itself.
EG, doesn't work:
int[] expectedResult = new int[] { 116800,…

mBria
- 2,412
- 2
- 15
- 11
111
votes
3 answers
assertAll vs multiple assertions in JUnit5
Is there any reason to group multiple assertions:
public void shouldTellIfPrime(){
Assertions.assertAll(
() -> assertTrue(isPrime(2)),
() -> assertFalse(isPrime(4))
);
}
instead of doing this:
public void…

Wilhelm Olejnik
- 2,382
- 3
- 14
- 21
109
votes
6 answers
Does R have an assert statement as in python?
a statement that checks if something is true and if not prints a given error message and exits

Dan
- 6,008
- 7
- 40
- 41
107
votes
5 answers
Eclipse: enable assertions
I'm running Eclipse Galileo. How do I enable assertions in Eclipse?
As suggested by other sites, I've tried adding the arguments: -ea. I have also tried changing the compiler compliance level to 1.4. Neither of those suggestions worked.

well actually
- 11,810
- 19
- 52
- 70
100
votes
6 answers
assert vs. JUnit Assertions
Today I saw a JUnit test case with a java assertion instead of the JUnit assertions—Are there significant advantages or disadvantages to prefer one over the other?

Robert
- 8,406
- 9
- 38
- 57
79
votes
4 answers
How to change the message in a Python AssertionError?
I'm writing per the following, in which I try to produce a decent error message when comparing two multiline blocks of Unicode text. The interior method that does the comparison raises an assertion, but the default explanation is useless to me
I…

Andres Jaan Tack
- 22,566
- 11
- 59
- 78
76
votes
17 answers
Avoiding unused variables warnings when using assert() in a Release build
Sometimes a local variable is used for the sole purpose of checking it in an assert(), like so -
int Result = Func();
assert( Result == 1 );
When compiling code in a Release build, assert()s are usually disabled, so this code may produce a warning…

Hexagon
- 6,845
- 2
- 24
- 16
75
votes
19 answers
Why should I use asserts?
I never got the idea of asserts -- why should you ever use them?
I mean, let's say I were a formula driver and all the asserts were things like security belt, helmet, etc.
The tests (in debug) were all okay, but now we want to do racing (release)!…
jan
65
votes
6 answers
How to enable the Java keyword assert in Eclipse program-wise?
How can I enable the assert keyword in Eclipse?
public class A
{
public static void main(String ... args)
{
System.out.println(1);
assert false;
System.out.println(2);
}
}

Siva Kumar Reddy G
- 1,274
- 3
- 18
- 32
61
votes
4 answers
How to implement XUnit descriptive Assert message?
Context
in XUnit github I found this: Add Assert.Equal(expected, actual, message) overload #350 (so a developer ask for a non existing overload see below)
Quote from the answer:
We are a believer in self-documenting code; that includes your…

g.pickardou
- 32,346
- 36
- 123
- 268
58
votes
7 answers
Java/ JUnit - AssertTrue vs AssertFalse
I'm pretty new to Java and am following the Eclipse Total Beginner's Tutorials. They are all very helpful, but in Lesson 12, he uses assertTrue for one test case and assertFalse for another. Here's the code:
// Check the book out to p1 (Thomas)
//…

Thomas
- 5,810
- 7
- 40
- 48
53
votes
11 answers
C compiler asserts - how to implement?
I'd like to implement an "assert" that prevents compilation, rather than failing at runtime, in the error case.
I currently have one defined like this, which works great, but which increases the size of the binaries.
#define…

NickB
- 1,471
- 4
- 14
- 20
52
votes
12 answers
Detecting whether on UI thread in WPF and Winforms
I've written an assertion method Ensure.CurrentlyOnUiThread(), below, that checks that the current thread is a UI thread.
Is this going to be reliable in detecting the Winforms UI thread?
Our app is mixed WPF and Winforms, how best to detect a…

chillitom
- 24,888
- 17
- 83
- 118
49
votes
2 answers
How to catch an assert with Google test?
I'm programming some unit test with the Google test framework. But I want to check whether some asserts are well placed and are useful. Is there a way to catch an assert in Google test?
Example code under test:
int factorial(int n){
assert(n >=…

Killrazor
- 6,856
- 15
- 53
- 69