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
58
votes
2 answers

NSAssert vs. assert: Which do you use, and when?

I've read two really interesting pieces of advice, recently: In the comments to this StackOverflow answer, @Mike Weller says to leave your asserts on in production code... what's the performance hit, really? Is there any reason NOT to leave them…
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
58
votes
9 answers

assert() with message

I saw somewhere assert used with a message in the following way: assert(("message", condition)); This seems to work great, except that gcc throws the following warning: warning: left-hand operand of comma expression has no effect How can I stop…
Alexandru
  • 25,070
  • 18
  • 69
  • 78
58
votes
2 answers

Rails ActiveSupport: How to assert that an error is raised?

I am wanting to test a function on one of my models that throws specific errors. The function looks something like this: def merge(release_to_delete) raise "Can't merge a release with itself!" if( self.id == release_to_delete.id ) raise "Can…
spilliton
  • 3,811
  • 5
  • 35
  • 35
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
58
votes
8 answers

Making Python's `assert` throw an exception that I choose

Can I make assert throw an exception that I choose instead of AssertionError? UPDATE: I'll explain my motivation: Up to now, I've had assertion-style tests that raised my own exceptions; For example, when you created a Node object with certain…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
56
votes
4 answers

Ambiguous method call Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match:

I am getting the following error: Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match For this line of code in my Junit tests, note that getScore() returns a double: assertEquals(2.5,…
java123999
  • 6,974
  • 36
  • 77
  • 121
56
votes
7 answers

Assert a good practice or not?

Is it a good practice to use Assert for function parameters to enforce their validity. I was going through the source code of Spring Framework and I noticed that they use Assert.notNull a lot. Here's an example public static ParsedSql…
ken
  • 3,745
  • 6
  • 34
  • 49
55
votes
8 answers

Is it bad practice to have more than one assertion in a unit test?

Is it bad practice to have more than one assertion in a unit test? Does it matter?
leora
  • 188,729
  • 360
  • 878
  • 1,366
55
votes
12 answers

How can I check if some text exist or not in the page using Selenium?

I'm using Selenium WebDriver, how can I check if some text exist or not in the page? Maybe someone recommend me useful resources where I can read about it. Thanks
khris
  • 4,809
  • 21
  • 64
  • 94
55
votes
4 answers

In what cases we need to include ?

In what cases should we include cassert?
Jane
  • 739
  • 1
  • 6
  • 10
53
votes
12 answers

Best practice for debug Asserts during Unit testing

Does heavy use of unit tests discourage the use of debug asserts? It seems like a debug assert firing in the code under test implies the unit test shouldn't exist or the debug assert shouldn't exist. "There can be only one" seems like a reasonable…
Steve Steiner
  • 5,299
  • 4
  • 32
  • 43
53
votes
7 answers

What is the use of Python's basic optimizations mode? (python -O)

Python has a flag -O that you can execute the interpreter with. The option will generate "optimized" bytecode (written to .pyo files), and given twice, it will discard docstrings. From Python's man page: -O Turn on basic optimizations. This…
u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
50
votes
3 answers

Test::Unit Rails - How to assert one number is greater than another one?

I am writing my first unit tests with Test::Unit and I have reached a point where I need to compare two numbers. Much to my surprise, I have discovered that none of the following were…
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
49
votes
8 answers

Pthread mutex assertion error

I'm encountering the following error at unpredictable times in a linux-based (arm) communications application: pthread_mutex_lock.c:82: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed. Google turns up a lot of references to that…
Dave Causey
  • 13,098
  • 7
  • 30
  • 26
48
votes
2 answers

Differences between Assert.True and Assert.IsTrue in NUnit?

Is there any differences between those two?
Vlad Titov
  • 686
  • 1
  • 6
  • 15