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
37
votes
6 answers

Why assertEquals and assertSame in junit return the same result for two instances same class?

According to documentation assertEquals() Asserts that two objects are equal. assertSame() Asserts that two objects refer to the same object. So I am expecting that if I have a class like below class SomeClass {} then SomeClass someClass1= new…
rawData
  • 729
  • 4
  • 10
  • 17
37
votes
5 answers

PHP and unit testing assertions with decimals

I have a method that returns a float like 1.234567890.I want to test that it really does so. However, it seems that this returned float has different precision on different platforms so how do I assert that the returned value is 1.23456789? If I…
Tower
  • 98,741
  • 129
  • 357
  • 507
37
votes
16 answers

Unit Testing without Assertions

Occasionally I come accross a unit test that doesn't Assert anything. The particular example I came across this morning was testing that a log file got written to when a condition was met. The assumption was that if no error was thrown the test…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
36
votes
4 answers

How does Assert.AreEqual determine equality between two generic IEnumerables?

I have a unit test to check whether a method returns the correct IEnumerable. The method builds the enumerable using yield return. The class that it is an enumerable of is below: enum TokenType { NUMBER, COMMAND, …
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
36
votes
5 answers

How to check if method has an attribute

I have an example class public class MyClass{ ActionResult Method1(){ .... } [Authorize] ActionResult Method2(){ .... } [Authorize] ActionResult Method3(int value){ .... } } Now what I…
4rchie
  • 1,244
  • 1
  • 15
  • 23
36
votes
4 answers

How to guide GCC optimizations based on assertions without runtime cost?

I have a macro used all over my code that in debug mode does: #define contract(condition) \ if (!(condition)) \ throw exception("a contract has been violated"); ... but in release mode: #define contract(condition) \ if…
LyingOnTheSky
  • 2,844
  • 1
  • 14
  • 33
34
votes
4 answers

How to check if value is nan in unittest?

I've got functions, which sometimes return NaNs with float('nan') (I'm not using numpy). How do I write a test for it, since assertEqual(nan_value, float('nan')) is just like float('nan') == float('nan') always false. Is there maybe something like…
tamasgal
  • 24,826
  • 18
  • 96
  • 135
34
votes
1 answer

Unit Testing: Assert that a file/path exists

I am attempting to create a regression test for my Installer. The regression test is a script written in Python. The test checks that the correct files have been installed in the correct place. Is there a way to assert that a file/folder exists? I…
sazr
  • 24,984
  • 66
  • 194
  • 362
33
votes
8 answers

Debug.Assert vs. Specific Thrown Exceptions

I've just started skimming 'Debugging MS .Net 2.0 Applications' by John Robbins, and have become confused by his evangelism for Debug.Assert(...). He points out that well-implemented Asserts store the state, somewhat, of an error condition,…
Nij
  • 2,028
  • 2
  • 22
  • 27
32
votes
11 answers

Is it good practice to use assert in Java?

I know that the keyword assert exists in java. However I don't remember seeing code that uses it. Probably I am using exceptions and logging in places where I could have used it. Is it a good practice to use the assert keyword in java? EDIT: I…
oshai
  • 14,865
  • 26
  • 84
  • 140
32
votes
6 answers

Is assert(false) ignored in release mode?

I am using VC++. Is assert(false) ignored in release mode?
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
32
votes
2 answers

Python : Assert that variable is instance method?

How can one check if a variable is an instance method or not? I'm using python 2.5. Something like this: class Test: def method(self): pass assert is_instance_method(Test().method)
quano
  • 18,812
  • 25
  • 97
  • 108
31
votes
6 answers

Using assertion in the Linux kernel

I have a question about assert() in Linux: can I use it in the kernel? If no, what techniques do you usually use if, for example I don't want to enter NULL pointer?
macindows
  • 4,303
  • 4
  • 18
  • 9
31
votes
8 answers

What are acceptable use-cases for python's `assert` statement?

I often use python's assert statement to check user input and fail-fast if we're in a corrupt state. I'm aware that assert gets removed when python with the -o(optimized) flag. I personally don't run any of my apps in optimized mode, but it feels…
Gattster
  • 4,613
  • 5
  • 27
  • 39
31
votes
3 answers

Groovy 'assert': How to display the value?

How do I display a value whether it is true or false in groovy? I'm using Eclipse as my IDE. assert 4 * ( 2 + 3 ) - 6 == 14 //integers only And also I don't understand 'assert' too well in Groovy. Is it like an if() statement/boolean in…
AppSensei
  • 8,270
  • 22
  • 71
  • 99