Questions tagged [assertions]
Assertions enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.
49 questions
0
votes
1
answer
271
views
Is interleaving local variable declarations with assertions and function calls a bad practice?
In my experience, it is customary to place local variable declarations at the beginning of their scope. Several questions in this forum ask whether this needs to be so, and their answers tend to agree ...
6
votes
7
answers
4k
views
Test A requires reviewing other files to prove it is correct. Test B doesn’t but has more than one assert
Say you want to test an update capability across a CRUD api.
Test A updates a field, queries it, and asserts that it now has the value from the update. Knowledge about the starting value (and that it ...
20
votes
8
answers
9k
views
Why assert for null on a object before asserting on some of its internals?
Let's consider the following test.
[Fact]
public void MyTest()
{
// Arrange Code
var sut = new SystemWeTest();
// Act Code
var response = sut.Request();
// Assert
...
-1
votes
1
answer
137
views
Software development in Python: unittests and assertions
I recently finished developing a piece of software in python where learning models and data processing procedures are contained within different class objects that succeed to each others (modules). As ...
-1
votes
2
answers
1k
views
Assert same and equals in unit test
I have a function to be tested
fn doNothing(Student student) {
//do some other operations here. but student is unmodified
return student;
}
And my unit test is
var student = new Student("...
14
votes
14
answers
8k
views
Should I raise an exception/error when an optional argument is used but is not necessary?
Take this constructed example:
def fetch_person(person_id, country_code=None):
if is_fully_qualified(person_id):
return person_source_1.fetch_person(person_id)
else:
return ...
6
votes
5
answers
8k
views
Assertion statements: Remove or retain in production code
I'm working in a large company and am doing my first Java project here. We have certain code style guidelines. In the Java guideline, it is said that "assertions are only intended for debugging ...
31
votes
9
answers
10k
views
"Dead programs tell no lies" in the context of GUI programs
In The Pragmatic Programmer, the authors write:
One of the benefits of detecting problems as soon as you can is that you can crash earlier, and crashing is often the best thing you can do. The ...
7
votes
5
answers
2k
views
Is it a good idea to start a function with a bunch of assert statements?
Sometimes when I look at other people's code I see functions that make a bunch of assumptions about the inputs but do not explicitly assert their assumptions. For example, look at the code below:
def ...
0
votes
2
answers
471
views
Why don't people usually use asserts throughout their application?
I usually do Javascript and I like putting console.assert liberally in my application. It throws an error if the first argument is falsey. E.g.:
console.assert(price > 0, 'Price isn\'t above 0')
...
4
votes
4
answers
503
views
Design pattern: method that checks consistency of object's internal state
I have got a project in C++. Each of my classes has a method void assert_good() const. The usage of that method is solely for debugging purposes: if the object is an inconsistent/impossible internal ...
3
votes
3
answers
153
views
Testing new feature in existing test?
A new feature is being developed.
I want to test this feature in different scenarios.
We have existing tests that correspond to this scenario (but they test other features).
Should I :
insert new ...
7
votes
4
answers
1k
views
Should I use assertions in a function that calls a function that have already them?
Let F(x) be a function that calls G(x), in which x must be greater than 0. If G already does assert(x > 0), should F do it as well?
0
votes
1
answer
844
views
Writing assertion for unit testing queue data structure
I have data structure queue with 2 operations:
typedef struct queue queue
void enqueue(queue *q, void *elem);
void *dequeue(queue *q);
queue *read_from_file(const char *path);
I want to write ...
1
vote
2
answers
34
views
Dealing with assumptions in modules that result in duplicate work
Imagine a game that needs to score words. A words needs to be scored immediately, in a list of words or even in a list of words from list of players.
I've created a scoring module which has the ...