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
1
vote
1 answer

Python - Why the use of assert(required_param)?

I found this today while looking at a library for an API . def my_function(self, required_param=None): assert(required_param) ... Do cool function stuff Wouldn't it be easier to do this: def my_function(self, required_param): ... Do…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
1
vote
0 answers

Swift / Playground - Assertions not working in Xcode 6.3.2?

The following code statement is not interpreted by the playground, resulting to display the initial constant values, but the assertion is never triggered. Am i missing something in my code, or is this a bug? let aPersonsAge = -10 assert(aPersonsAge…
user4030492
1
vote
1 answer

assert not working on android 5.1.1?

My code uses assert calls extensively. After updating to 5.1.1 (tested on Nexus 4 & Nexus 5), the assertion calls are being ignored. Selecting "debug app" under Developer options made no difference. Had anyone solve this issue? (Before I'm forced…
Amir Uval
  • 14,425
  • 4
  • 50
  • 74
1
vote
1 answer

chai.assert() wont run methods in a test before the assertion (chai assert lib with protractor)

First time I post an issue on SO, I hope I'm doing it right. it (' :: 2.0 service creation :: should fill out service info tab', function(){ createNewService.setServiceName(e2eConfig.newServiceDetails.basicServiceName); …
balerionLv
  • 13
  • 2
1
vote
0 answers

Python, UUID, MySQL and Autobah/Twisted = assert error?

As I'm developing my app I've encountered an interesting situation. I need a session token so after some research I decided to opt for: sessionToken = uuid.UUID(bytes = M2Crypto.m2.rand_bytes(16)) I store this in MySQL table, as VARCHAR When I read…
Kostas
  • 367
  • 1
  • 3
  • 17
1
vote
2 answers

C# How I can expect a exception in Assert.AreEqual?

Example: Assert.AreEqual(**null**, Program.nDaysMonth(5, -10), "Error nDaysMonth, Month may -10."); I expect a exception. How I can expect a exception in Assert.AreEqual? Thanks.
Jesús
  • 13
  • 4
1
vote
1 answer

how to assert objects are equal using hamcrest

I want to write test where it checks if two objects are same. When the assert fails I want to know what fields are same and which are not. I can do assert on each of the field but was wondering if there is a way to compere the objects. Truck…
ankit s.
  • 93
  • 6
1
vote
4 answers

Is it good practice to use assert() in class mutators?

For example: void Date::month(unsigned int inMonth) { assert(inMonth <= 12); _month = inMonth; } If this is not good practice, what is the correct way to go about this?
cactusbin
  • 671
  • 1
  • 6
  • 11
1
vote
3 answers

Debug.Assert-s use the same error message. Should I promote it to a static variable?

I love Asserts and code readability but not code duplication, and in several places I use a Debug.Assert which checks for the same condition like so: Debug.Assert(kosherBaconList.SelectedIndex != -1, "An error message along the lines - you should…
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
1
vote
1 answer

How to assert if the image is zoomed in selenium

Using selenium with java I want to vary whether an image is zoomed when the cursor is moved over the image. From the source code below, element changes: Before zoom:
After zoom:
1
vote
0 answers

assert.deepEqual does not seem to work correctly

I want to test a serialize function of a nodejs module I've written. In my tests I have the following strange behavior: assert.equal(JSON.stringify(obj1), JSON.stringify(obj2)); // => TEST OK assert.deepEqual(obj1, obj2); // => TEST FAILS I have no…
Frank Roth
  • 6,191
  • 3
  • 25
  • 33
1
vote
1 answer

Assert.AreEqual<> doesn’t call my operator== overloaded function nor my Equal member function?

I have overloaded several compare operators like operator== in my class CXmlAttr. Then I wrote generic functions where I used these operators and was shocked that this didn’t work in C# or not in every case. Then I recoded some generic functions to…
melgoth
  • 11
  • 4
1
vote
1 answer

assertTrue multiple values in webdriver java

I want to assert.true 2 different value, one or the other. I am getting the value of the background color, which is being changed, so it can have two different colors as the option. This is what I have: Assert.assertTrue(Assert_BG1.contains("0,…
Elsid
  • 243
  • 2
  • 10
  • 25
1
vote
1 answer

Django [AssertionError: 400 != 201]

I'm a french guy and I'm working on Django. I beggin and I have a error. def setUp(self): self.username = 'admin' self.password = 'admin' self.datetime = 'Tue, 15 Nov 1994 08:12:31 GMT' self.temperature = '15.6' self.presence =…
Xam
  • 11
  • 2
1
vote
2 answers

How to assert in a JPM test

My synthetic test: exports.testAssertObject = function(assert) { console.log(arguments); assert.ok(assert, "Assert object exists"); }; fails in JPM with TypeError: assert.ok is not a function: $jpm --version 0.0.29 $jpm test JPM undefined…
Basilevs
  • 22,440
  • 15
  • 57
  • 102
1 2 3
99
100