Skip to main content

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
0 votes
1 answer
272 views

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 ...
Severo Raz's user avatar
6 votes
7 answers
4k views

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 ...
candied_orange's user avatar
20 votes
8 answers
9k views

Let's consider the following test. [Fact] public void MyTest() { // Arrange Code var sut = new SystemWeTest(); // Act Code var response = sut.Request(); // Assert ...
BAmadeusJ's user avatar
  • 326
-1 votes
1 answer
137 views

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 ...
JrCaspian's user avatar
  • 125
-1 votes
2 answers
1k views

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("...
Santhosh Thamaraiselvan's user avatar
14 votes
14 answers
8k views

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 ...
André Christoffer Andersen's user avatar
6 votes
5 answers
8k views

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 ...
Green 绿色's user avatar
31 votes
9 answers
10k views

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 ...
samfrances's user avatar
  • 1,095
7 votes
5 answers
2k views

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 ...
jss367's user avatar
  • 263
0 votes
2 answers
481 views

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') ...
Leo Jiang's user avatar
  • 117
4 votes
4 answers
507 views

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 ...
shuhalo's user avatar
  • 231
3 votes
3 answers
156 views

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 ...
Wenneguen's user avatar
  • 139
7 votes
4 answers
1k views

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?
Martel's user avatar
  • 615
0 votes
1 answer
845 views

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 ...
Some Name's user avatar
  • 121
1 vote
2 answers
34 views

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 ...
Sven's user avatar
  • 186
32 votes
9 answers
26k views

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 ...
Nobody moving away from SE's user avatar
18 votes
1 answer
3k views

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 ...
SerG's user avatar
  • 502
4 votes
3 answers
709 views

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 ...
gaazkam's user avatar
  • 4,529
1 vote
3 answers
4k views

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 ...
Jason's user avatar
  • 469
0 votes
1 answer
234 views

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) ...
Flowers4Life's user avatar
1 vote
0 answers
64 views

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 ...
Attilio's user avatar
  • 487
2 votes
1 answer
459 views

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 NOT ...
hellboy's user avatar
  • 141
6 votes
1 answer
291 views

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 ...
wefwefa3's user avatar
  • 1,007
4 votes
4 answers
919 views

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::...
user3998276's user avatar
5 votes
2 answers
5k views

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->...
bashrc's user avatar
  • 161
4 votes
1 answer
1k views

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 ...
Marcus Junius Brutus's user avatar
1 vote
3 answers
2k views

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)....
deamon's user avatar
  • 886
3 votes
2 answers
3k views

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 ...
Peter Bridger's user avatar
3 votes
1 answer
1k views

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. "...
DaveH's user avatar
  • 143
6 votes
1 answer
1k views

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 ...
chtenb's user avatar
  • 361
27 votes
3 answers
35k views

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 ...
Vader's user avatar
  • 395
0 votes
1 answer
247 views

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 ...
Christopher's user avatar
11 votes
1 answer
5k views

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. ...
warvariuc's user avatar
  • 352
1 vote
2 answers
3k views

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 ...
EdvRusj's user avatar
  • 633
2 votes
2 answers
521 views

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 ...
Angelo.Hannes's user avatar
5 votes
2 answers
593 views

Or more generally: to trigger compiler errors in test projects if something that can be checked at compile time is wrong?
stijn's user avatar
  • 4,168
5 votes
3 answers
869 views

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 {...
Tilak's user avatar
  • 239
28 votes
3 answers
11k views

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?
Dominik Fretz's user avatar
22 votes
9 answers
4k views

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 ...
Flo's user avatar
  • 1,241
1 vote
3 answers
889 views

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 ...
Justin Pihony's user avatar
3 votes
3 answers
269 views

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 ...
Mike's user avatar
  • 635
9 votes
5 answers
867 views

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 ...
GeT's user avatar
  • 193
10 votes
2 answers
652 views

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 ...
stijn's user avatar
  • 4,168
13 votes
5 answers
4k views

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 (...
Anon's user avatar
  • 325
37 votes
9 answers
8k views

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 ...
Alan Turing's user avatar
  • 1,533
2 votes
5 answers
864 views

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 ...
gablin's user avatar
  • 17.6k
57 votes
3 answers
17k views

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 ...
dsimcha's user avatar
  • 17.3k
84 votes
6 answers
40k views

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, ...
gablin's user avatar
  • 17.6k
545 votes
18 answers
497k views

In the comment to this great post, Roy Osherove mentioned the OAPT project that is designed to run each assert in a single test. The following is written on the project's home page: Proper unit tests ...