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.

Filter by
Sorted by
Tagged with
3
votes
3answers
121 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
4answers
266 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
1answer
67 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
2answers
20 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 ...
15
votes
8answers
6k views

Should there be assertions in release builds

The default behavior of assert in C++ is to do nothing in release builds. I presume this is done for performance reasons and maybe to prevent users from seeing nasty error messages. However, I'd ...
18
votes
1answer
2k views

Unit tests: deferred assertions with Linq

Is it ok to add deferred assertions like this var actualKittens = actualKittens.Select(kitten => { Assert.IsСute(kitten); return kitten }); Why? So I can iterate just once even with ...
4
votes
3answers
273 views

Why should we never `assert` that a library call doesn't fail with a certain error code?

From man 3 assert_perror: The purpose of the assert macros is to help programmers find bugs in their programs, things that cannot happen unless there was a coding mistake. However, with system or ...
1
vote
3answers
1k views

When to use asserts vs unit tests or both?

Background: I'm creating my first C++ game project and I plan on calling a InitialThirdPartySystems() function to...well...intialize all third party systems to be used in my code lol. Within it I ...
0
votes
1answer
119 views

Generic Asserts in Unit Tests

I've been dealing with a Sr Dev recently that loves unit testing. That's great! I'm not sure I agree with his testing methods though, contrived example: function foo (String $var) { switch ($var) ...
1
vote
0answers
36 views

Pros and cons of asserting on the DB level in selenium tests

Consider a Selenium test that does some steps on the UI (e.g. fill a field and then click a save button), and afterwards checks with a DB query if the data entered in the new field was really saved ...
3
votes
1answer
162 views

Dealing with conditional verification logic using guard assertion

I have parametrized test with 2 variants: NULL value and any NOT NULL value From this value is depends one assert: In case NULL it should be checked if object has field1 with value1 In case ...
6
votes
1answer
200 views

Strategies to increasing the maintainability of assertions in code [closed]

Background I am writing a compiler for a custom language for a school project and it is going really well for me. If I where to start all over from scratch I would have done many software ...
4
votes
4answers
298 views

Purpose of assertion messages

Some languages and libraries can associate assertion messages with assertions. Such assertion messages are included in error messages when the assertion does not hold. Examples are Java, Class::...
4
votes
2answers
1k views

When is it ok to assert for a pointer being non-null?

This came up as part of a code review for a code segment which resembled this: auto somePikachu = GetMeAPikachu(); NT_ASSERT(somePikachu != nullptr); // this only fires on debug build somePikachu->...
4
votes
1answer
576 views

sanity checks: when do they become paranoid checks [closed]

I normally add lots of sanity check clauses in my code. In other words, lots of assertions where the code is instructed to "panic" (usually print as much information as possible when a certain ...
0
votes
3answers
160 views

Is it good style to check explicitly for null in unit tests?

Whether or not a value is null could be checked implicitly like this: assertThat(value).isEqualTo("something"); Or it could be checked explicitly: assertThat(value).isNotNull(); assertThat(value)....
2
votes
2answers
1k views

How to (or should I) have one assert per test with object comparisons? [duplicate]

I've recently read The Art of Unit Testing by Roy Osherove which I found very useful for helping me establish how to define a good unit test. One key aspect of the guidelines Roy puts forward are to ...
3
votes
1answer
900 views

Assert Message in Unit Tests

I've just begun reading "The Art Of Unit Testing" by Roy Osherove, and while I'm mostly finding the material very helpful, he makes a statement about not using messages in your Assert statements. ...
6
votes
1answer
757 views

Advantages of using the library PyContracts over assert statements

Today I stumbled upon the python package called PyContracts. However, python has the assert statement which seems to allow you to do exactly those things. What advantages do contracts have over assert ...
12
votes
3answers
20k views

Python - assert vs if & return

I am writing a script that does something to a text file (what it does is irrelevant for my question though). So before I do something to the file I want to check if the file exists. I can do this, no ...
0
votes
1answer
128 views

Using a flag in in-memory-repository to indicate update to entity

I'm about to write a lot of test for my application. While some of the tests check whether everything works if the user enters the right data, most of them deal with wrong/missing data. In these cases ...
10
votes
1answer
4k views

Duck typing, data validation and assertive programming in Python

About duck typing: Duck typing is aided by habitually not testing for the type of arguments in method and function bodies, relying on documentation, clear code and testing to ensure correct use. ...
1
vote
2answers
2k views

Assertions vs Exceptions - is my understanding of the differences between the two correct? [duplicate]

Design By Contract uses preconditions and postconditions of the public methods in a class together to form a contract between the class and its clients. a) In code we implement preconditions and ...
2
votes
2answers
299 views

About the usage of assertions [duplicate]

I stumbled upon an article named Programming With Assertions. And beside the mechanism of turning on and off assertions after compile time, I don't get it. Why were assertions introduced on language ...
5
votes
2answers
443 views

Is it ok to use static (compile-time) assertions in unit tests?

Or more generally: to trigger compiler errors in test projects if something that can be checked at compile time is wrong?
5
votes
3answers
678 views

How much to test in TDD?

I am newbie to TDD (writing first project following TDD practices). I have fairly basic interface IProfiler and an implementation Profiler. interface IProfiler { bool IsBusy {get;} long Elapsed {...
22
votes
3answers
9k views

Should I still use Debug.Assert today?

I recently came across some newly written code that was interspersed with lots of Debug.Assert (C#). Should we still use this widely despite the usage of TDD, BDD and Unit Testing in general?
19
votes
9answers
2k views

Are too many assertions code smell?

I've really fallen in love with unit testing and TDD - I am test infected. However, unit testing is normally used for public methods. Sometimes though I do have to test some assumptions-assertions ...
1
vote
3answers
626 views

Is Debug.Assert obsolete if you write unit tests?

Just like the question asks, is there a need to add Debug.Assert into your code if you are writing unit tests (which has its own assertions)? I could see that this might make the code more obvious ...
3
votes
3answers
229 views

Should I create my own Assert class based on these reasons?

The main reason I don't like Debug.Assert is the fact that these assertions are disabled in Release. I know that there's a performance reason for that, but at least in my situation I believe the gains ...
9
votes
5answers
713 views

Asserting in public methods [duplicate]

I'm a fan of using assertions in the code to check for preconditions. The question is whether is it wise to use assertions in public methods to check the precondition? My personal opinion is, that if ...
10
votes
2answers
591 views

code contracts/asserts: what with duplicate checks?

I'm a huge fan of writing asserts, contracts or whatever type of checks available in the language I'm using. One thing that bothers me a bit is that I'm not sure what the common practice is for ...
13
votes
5answers
3k views

How can I improve my error checking and handling?

Lately I have been struggling to understand what the right amount of checking is and what the proper methods are. I have a few questions regarding this: What is the proper way to check for errors (...
33
votes
9answers
4k views

Is possible to write too many asserts?

I am a big fan of writing assert checks in C++ code as a way to catch cases during development that cannot possibly happen but do happen because of logic bugs in my program. This is a good practice in ...
2
votes
5answers
314 views

Assertions in private functions - Where to draw the line?

We use assertions to check for illegal behaviour which just shouldn't happen if everything is working as it should be, such as using NULL as argument when it clearly shouldn't. This is all very well ...
51
votes
3answers
10k views

Are asserts or unit tests more important?

Both asserts and unit tests serve as documentation for a codebase, and a means of discovering bugs. The main differences are that asserts function as sanity checks and see real inputs, whereas unit ...
36
votes
4answers
7k views

Using assertions versus throwing exceptions?

Often when I write a functions I want to make sure the inputs to it are valid in order to detect such errors as early as possible (I believe these are called preconditions). When a precondition fails, ...