Questions tagged [assertion]

An assertion is a software construct where the developer states ("asserts") a condition that he believes will always be true. If the condition evaluates to false in some languages an exception is thrown, in others a message is printed, and in others the program ceases to operate.

An assertion is a software construct where the developer states ("asserts") a condition that he believes will always be true. If the condition evaluates to false, in some languages an exception is thrown, in others a message is printed, and in still others the program ceases to operate.

Assertions can exist in most high-level languages (C, Java, etc...) as well as in register-transfer-languages (RTL) such as Verilog, System Verilog, and VHDL.

1445 questions
1430
votes
23 answers

What is the use of "assert" in Python?

What does assert mean? How is it used?
Hossein
  • 40,161
  • 57
  • 141
  • 175
717
votes
20 answers

What are assertions in Java and when should they be used?

What are some real life examples to understand the key role of the Java assert keyword?
Praveen
  • 90,477
  • 74
  • 177
  • 219
590
votes
15 answers

Best practice for using assert?

Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes? Is assert x >= 0, 'x is less than zero' better or worse than if x < 0: raise Exception('x is less…
meade
  • 22,875
  • 12
  • 32
  • 36
387
votes
13 answers

How do I assert my exception message with JUnit Test annotation?

I have written a few JUnit tests with @Test annotation. If my test method throws a checked exception and if I want to assert the message along with the exception, is there a way to do so with JUnit @Test annotation? AFAIK, JUnit 4.7 doesn't provide…
Cshah
  • 5,612
  • 10
  • 33
  • 37
311
votes
9 answers

How do I use Assert.Throws to assert the type of the exception?

How do I use Assert.Throws to assert the type of the exception and the actual message wording? Something like this: Assert.Throws( ()=>user.MakeUserActive()).WithMessage("Actual exception message") The method I am testing throws…
epitka
  • 17,275
  • 20
  • 88
  • 141
255
votes
20 answers

When should I use Debug.Assert()?

I've been a professional software engineer for about a year now, having graduated with a CS degree. I've known about assertions for a while in C++ and C, but had no idea they existed in C# and .NET at all until recently. Our production code contains…
138
votes
8 answers

What does the "assert" keyword do?

What does assert do? For example in the function: private static int charAt(String s, int d) { assert d >= 0 && d <= s.length(); if (d == s.length()) return -1; return s.charAt(d); }
Peiska
  • 1,789
  • 4
  • 17
  • 15
132
votes
12 answers

When to use an assertion and when to use an exception

Most of the time I will use an exception to check for a condition in my code, I wonder when it is an appropriate time to use an assertion? For instance, Group group=null; try{ group = service().getGroup("abc"); }catch(Exception e){ //I dont…
cometta
  • 35,071
  • 77
  • 215
  • 324
111
votes
8 answers

Why do I get a C malloc assertion failure?

I am implementing a divide and conquer polynomial algorithm so I can benchmark it against an OpenCL implementation, but I can't get malloc to work. When I run the program, it allocates a bunch of stuff, checks some things, then sends the size/2 to…
Chris
  • 4,425
  • 5
  • 34
  • 49
104
votes
4 answers

What is the expected syntax for checking exception messages in MiniTest's assert_raises/must_raise?

What is the expected syntax for checking exception messages in MiniTest's assert_raises/must_raise? I'm trying to make an assertion something like the following, where "Foo" is the expected error message: proc { bar.do_it }.must_raise…
kfitzpatrick
  • 2,425
  • 4
  • 19
  • 11
103
votes
8 answers

Debug.Assert vs Exception Throwing

I've read plenty of articles (and a couple of other similar questions that were posted on StackOverflow) about how and when to use assertions, and I understood them well. But still, I don't understand what kind of motivation should drive me to use…
unknown
102
votes
11 answers

AssertEquals 2 Lists ignore order

That should be really simple question I believe. But somehow I can't find answer in Google. Assume that I have 2 Lists of Strings. First contains "String A" and "String B", second one contains "String B" and "String A" (notice difference in order).…
kukis
  • 4,489
  • 6
  • 27
  • 50
87
votes
5 answers

CollectionAssert in jUnit?

Is there a jUnit parallel to NUnit's CollectionAssert?
ripper234
  • 222,824
  • 274
  • 634
  • 905
86
votes
7 answers

How to assert that an iterable is not empty on Unittest?

After submitting queries to a service, I get a dictionary or a list back and I want to make sure it's not empty. I using Python 2.7. I am surprised of not having any assertEmpty method for the unittest.TestCase class instance. The existing…
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
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
1
2 3
96 97