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
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
102
votes
5 answers

Python/Django: how to assert that unit test result contains a certain string?

In a python unit test (actually Django), what is the correct assert statement that will tell me if my test result contains a string of my choosing? self.assertContainsTheString(result, {"car" : ["toyota","honda"]}) I want to make sure that my…
user798719
  • 9,619
  • 25
  • 84
  • 123
101
votes
4 answers

Proper way to assert type of variable in Python

In using a function, I wish to ensure that the type of the variables are as expected. How to do it right? Here is an example fake function trying to do just this before going on with its role: def my_print(begin, text, end): """Print 'text' in…
Morlock
  • 6,880
  • 16
  • 43
  • 50
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
97
votes
3 answers

Where are the Assertion Methods list from Django TestCase?

I googled to find the assert methods list. But it seems like this documentation is very well hidden. Does anyone know where it is?
btk
  • 3,158
  • 2
  • 29
  • 30
96
votes
2 answers

How to handle AssertionError in Python and find out which line or statement it occurred on?

I want to handle AssertionErrors both to hide unnecessary parts of the stack trace from the user and to print a message as to why the error occurred and what the user should do about it. Is there any way to find out on which line or statement the…
devtk
  • 1,999
  • 2
  • 18
  • 24
93
votes
5 answers

How can I completely disable calls to assert()?

My code is full of calls to assert(condition). In the debug version I use g++ -g which triggers my assertions. Unexpectedly, the same assertions are also triggered in my release version, the one compiled without -g option. How can I completely…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
92
votes
9 answers

What's the difference between raise, try, and assert?

I have been learning Python for a while and the raise function and assert are (what I realised is that both of them crash the app, unlike try - except) really similar and I can't see a situation where you would use raise or assert over try. So, what…
Defneit
  • 1,041
  • 1
  • 8
  • 6
92
votes
6 answers

Why does Assert.AreEqual(T obj1, Tobj2) fail with identical byte arrays

I have two identical byte arrays in the following segment of code: /// ///A test for Bytes /// [TestMethod()] public void BytesTest() { byte[] bytes =…
David Anderson
  • 13,558
  • 5
  • 50
  • 76
91
votes
8 answers

Should I be using assert in my PHP code?

A coworker has added the assert command a few times within our libraries in places where I would have used an if statement and thrown an exception. (I had never even heard of assert before this.) Here is an example of how he used…
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
90
votes
5 answers

Is it idiomatic Ruby to add an assert( ) method to Ruby's Kernel class?

I'm expanding my Ruby understanding by coding an equivalent of Kent Beck's xUnit in Ruby. Python (which Kent writes in) has an assert() method in the language which is used extensively. Ruby does not. I think it should be easy to add this but is…
Andrew Harmel-Law
  • 7,739
  • 12
  • 44
  • 55
89
votes
9 answers

Can I use assert on Android devices?

I want to use the Assert keyword in my android apps to destroy my app in some cases on the emulator, or my device during testing. Is this possible? It seems that the emulator just ignores my asserts.
Janusz
  • 187,060
  • 113
  • 301
  • 369
88
votes
4 answers

Can java's assert statement allow you to specify a message?

Seems likes it might be useful to have the assert display a message when an assertion fails. Currently an AssertionError gets thrown, can you specify a custom message for it? Can you show an example mechanism for doing this (other than creating your…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
82
votes
3 answers

PHPUnit: assertInstanceOf() not working when passing type as second argument as non-string

I need to check if a variable is an object of the User type. User is my class $user my object $this->assertInstanceOf($user, User); This is not working. I have a use of undefined constant User - assumed 'User'.
user1173169
81
votes
2 answers

Diff between Assert.AreEqual and Assert.AreSame?

What is the difference between Assert.AreEqual and Assert.AreSame?
Pramuka
  • 1,064
  • 1
  • 8
  • 15