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

How to use assert in android?

I want to use assert obj != null : "object cannot be null" on Android device. The assert doesn't seem to work, so I searched online and I found this local solution: adb shell setprop debug.assert 1 it does work on my local machine. I want to run…
Adibe7
  • 3,469
  • 7
  • 30
  • 36
73
votes
5 answers

Why assert is not largely used?

I found that Python's assert statement is a good way to catch situations that should never happen. And it can be removed by Python optimization when the code is trusted to be correct. It seems to be a perfect mechanism to run Python applications in…
Carlo Pires
  • 4,606
  • 7
  • 32
  • 32
73
votes
11 answers

JUnit Assert with BigDecimal

I want to use assert between 2 two decimal, I use this: BigDecimal bd1 = new BigDecimal (1000); BigDecimal bd2 = new BigDecimal (1000); org.junit.Assert.assertSame (bd1,bd2); but the JUnit log shows: expected <1000> was not: <1000>
kAnGeL
  • 743
  • 1
  • 5
  • 8
73
votes
6 answers

What is the difference between assert and static_assert?

I know that static_assert makes assertions at compile time, and assert - at run time, but what is the difference in practice? As far as I understand, deep down they are pieces of code, like if (condition == false) exit(); Can someone give me an…
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
72
votes
3 answers

Unittest's assertEqual and iterables - only check the contents

Is there a 'decent' way in unittest to check the equality of the contents of two iterable objects? I am using a lot of tuples, lists and numpy arrays and I usually only want to test for the contents and not for the type. Currently I am simply…
Lucas Hoepner
  • 1,437
  • 1
  • 16
  • 21
71
votes
10 answers

Exception Vs Assertion

What is the difference between Java exception handling and using assert conditions? It's known that Assert is of two types. But when should we use assert keyword?
JavaResp
  • 2,253
  • 5
  • 24
  • 25
70
votes
4 answers

Boolean Expressions in SQL Select list

I want to create a SQL Select to do a unit test in MS SQL Server 2005. The basic idea is this: select 'Test Name', foo = 'Result' from bar where baz = (some criteria) The idea being that, if the value of the "foo" column is "Result", then I'd get a…
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
68
votes
5 answers

How to format a python assert statement that complies with PEP8?

How does one format a long assert statement that complies with PEP8? Please ignore the contrived nature of my example. def afunc(some_param_name): assert isinstance(some_param_name, SomeClassName), 'some_param_name must be an instance of…
stantonk
  • 1,922
  • 1
  • 18
  • 24
68
votes
4 answers

design of python: why is assert a statement and not a function?

In Python, assert is a statement, and not a function. Was this a deliberate decision? Are there any advantages to having assert be a statement (and reserved word) instead of a function? According to the docs, assert expression1, expression2 is…
cberzan
  • 2,009
  • 1
  • 21
  • 32
66
votes
9 answers

Adding message to assert

I'm looking for a way to add custom messages to assert statements. I found this questions Add custom messages in assert? but the message is static there. I want to do something like this: assert((0 < x) && (x < 10), std::string("x was ") +…
tauran
  • 7,986
  • 6
  • 41
  • 48
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
64
votes
6 answers

What's the difference between Assert.AreNotEqual and Assert.AreNotSame?

In C#, what's the difference between Assert.AreNotEqual and Assert.AreNotSame
Dan Esparza
  • 28,047
  • 29
  • 99
  • 127
64
votes
5 answers

Why is assert a macro and not a function?

My lecturer has asked me that in class, and I was wondering why is it a macro instead of a function?
Itai Bar
  • 747
  • 5
  • 14
59
votes
3 answers

xUnit Equivalent of MSTest's Assert.Inconclusive

What is the xUnit equivalent of the following MSTest code: Assert.Inconclusive("Reason"); This gives a yellow test result instead of the usual green or red. I want to assert that the test could not be run due to certain conditions and that the test…
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
59
votes
4 answers

Will an assertion error be caught by in a catch block for java exception?

Code:- try { Assert.assertEquals("1", "2"); } catch (Exception e) { System.out.println("I am in error block"); } If the assert statements fails, I would like to capture the error in the catch block. I am trying with the above code and its…
Galet
  • 5,853
  • 21
  • 82
  • 148