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
47
votes
3 answers

What is the meaning of an assumption in scala compared to an assertion?

Scala seems to define 3 kinds of assertions: assert, require and assume. As far as I can understand, the difference (compared to a generic assertion) of require is that it is specifically meant for checking inputs (arguments, incoming messages etc).…
Ivan
  • 63,011
  • 101
  • 250
  • 382
47
votes
15 answers

How to assert an actual value against 2 or more expected values?

I'm testing a method to see if it returns the correct string. This string is made up of a lot of lines whose order might change, thus usually giving 2 possible combinations. That order is not important for my application. However, because the order…
Alex
  • 7,432
  • 20
  • 75
  • 118
47
votes
3 answers

Python's assert_called_with, is there a wildcard character?

Suppose I have a class in python set up like this. from somewhere import sendmail class MyClass: def __init__(self, **kargs): self.sendmail = kwargs.get("sendmail", sendmail) #if we can't find it, use imported def def…
Zack
  • 13,454
  • 24
  • 75
  • 113
45
votes
2 answers

How to check constructor arguments and throw an exception or make an assertion in a default constructor in Scala?

I would like to check constructor arguments and refuse to construct throwing IllegalArgumentException in case the arguments set is not valid (the values don't fit in expected constraints). How to code this in Scala?
Ivan
  • 63,011
  • 101
  • 250
  • 382
45
votes
8 answers

How to find the name of the current function at runtime?

After years of using the big ugly MFC ASSERT macro, I have finally decided to ditch it and create the ultimate ASSERT macro. I am fine with getting the file and line number, and even the expression that failed. I can display a messagebox with these…
demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
45
votes
5 answers

Assert keyword in Java

Do you use the assert keyword or throw some validation runtime exception? What benefits does it give to you or why do you think it's not worth it to use?
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
45
votes
2 answers

Debug.Assert vs Code Contract usage

When should I debug.assert over code contracts or vice versa? I want to check precondition for a method and I am confused to choose one over the other. I have unit tests where I want to test failure scenarios and expect exceptions. Is it a good…
Carbine
  • 7,849
  • 4
  • 30
  • 54
44
votes
3 answers

What are the advantages or difference in “assert False” and “self.assertFalse”

I am writing tests and I have heard some people saying to use self.assertFalse rather than assert False. Why is this and are there any advantages to be had?
chrisg
  • 40,337
  • 38
  • 86
  • 107
39
votes
5 answers

Node.js assert.throws with async functions (Promises)

I want to check if an async function throws using assert.throws from the native assert module. I tried with const test = async () => await aPromise(); assert.throws(test); // AssertionError: Missing expected exception.. It (obviously?) doesn't work…
seldon
  • 977
  • 2
  • 8
  • 20
39
votes
4 answers

What does assert(0) mean?

I had a question like this on one of my exams and I'm still not too sure how to answer it. I understand that assertions are ways to test your program, however I'm not too sure what assert(0) is checking. Is this a trick question? It will always…
Smith Western
  • 393
  • 1
  • 3
  • 5
39
votes
4 answers

What is the use of the "-O" flag for running Python?

Python can run scripts in optimized mode (python -O) which turns off debugs, removes assert statements, and IIRC it also removes docstrings. However, I have not seen it used. Is python -O actually used? If so, what for?
zaharpopov
  • 16,882
  • 23
  • 75
  • 93
39
votes
4 answers

Verifying ArgumentException and its message in Nunit , C#

In my test program in Nunit, I want to verify that it's getting the write Argument Exception by verifying the message. [Test] public void ArgumentsWorkbookNameException() { const string workbookName = "Tester.xls"; var…
Henry Zhang
  • 391
  • 1
  • 3
  • 3
38
votes
1 answer

Does CMAKE_BUILD_TYPE=Release imply -DNDEBUG?

Does CMAKE_BUILD_TYPE=Release implicitly imply -DNDEBUG? If not: isn't it reasonable to expect that this implication takes place? I want to know if following CMake code is redundant in my CMakeLists.txt: if (NOT CMAKE_BUILD_TYPE MATCHES Debug) …
patryk.beza
  • 4,876
  • 5
  • 37
  • 56
37
votes
6 answers

Should you assert not null with the assert statement in production code?

I've seen this question but have a few more questions about the usage of the assert keyword. I was debating with a few other coders about using assert. For this use case, there was a method that can return null if certain prerequisites are met. The…
Big_Bad_E
  • 947
  • 1
  • 12
  • 23
37
votes
7 answers

Is there a way of having something like jUnit Assert message argument in Mockito's verify method?

Let's assume a snippet of testing code: Observable model = Class.forName(fullyQualifiedMethodName).newInstance(); Observer view = Mockito.mock(Observer.class); model.addObserver(view); for (Method method :…
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148