Live data from Hacker News

Multiple assertions are fine in a unit test

stackoverflow.blog

111–120 of 348 posts

Re: Multiple assertions are fine in a unit test

#111
post #4

Wait - people are doing real http calls in unit tests, over a network, and complaining about multiple asserts in the test code?

It's not unusual to spin up a local dev server in the same environment (or on the same machine, ie at localhost) as the tests. There's an argument to say these aren't "unit" tests but your definition of "unit" may vary.

Re: Multiple assertions are fine in a unit test

#113
also, only testing public interfaces is perfectly fine and may actually be preferable as it leaves you free to refactor internals freely without breaking tests.

tbh unit testing is a balancing act between reaping code quality benefits and bogging yourself down with too much testing updating.

Re: Multiple assertions are fine in a unit test

#114

> The excellent book xUnit Test Patterns describes a test smell named Assertion Roulette. It describes situations where it may be difficult to determine exactly which assertion caused a test failure. How is that even possible in the first place? The entire job of an assertion is to wave a flag saying "here! condition failed!". In programming languages and test frameworks I worked with, this typically includes providi…

xUnit is terrible. It has a horrible culture, the author seems to have a god complex. It is overly complex and opinionated in the worst way possible. Many times I searched for 'how do I do something with xUnit' and found a github issue with people struggling with the same thing, and the author flat out refusing to incorporate the feature as it was against his principles. Other times I found that I needed to do was ov…

NUnit isn't much better. You'd think that they would have good test coverage and therefore high confidence to make changes, especially to fix actual bugs, but I gave up trying to get patches in because the core devs seem so afraid of breaking anything, even when it's an obviously-isolated private, hidden bug fix or performance improvement.

"We can't land this tiny fix because we have a release planned within three months" sort of thing.

Re: Multiple assertions are fine in a unit test

#115
I didn't know you were only supposed to put one assertion per unit test. some of mine have 5-10 assertions. why?. because I don't have time to write tests all day but I recognize there are tests. As long as it catches the bugs before it goes to prod and can be modified easily, who cares?

Re: Multiple assertions are fine in a unit test

#116
An assert message says what went wrong, and on which code line. How on earth does it help to make just one? The arrange part might take seconds for a nontrivial test and that would need to be duplicated both in code and execution time to make two asserts.

If you painstakingly craft a scenario where you create a rectangle of a specific expected size why wouldn’t it be acceptable to assert both the width and height of the the rectangle after you have created it?

assert_equal(20, w, …

assert_equal(10, h, …

A dogmatic rule would just lead to an objectively worse test where you assert an expression containing both width and height in a single assert?

assert_true(w == 20 && h == 10,…)

So I can only assume the rule also prohibits any compound/Boolean expressions in the asserts then? Otherwise you can just combine any number of asserts into one (including mutating state within the expression itself to emulate multiple asserts with mutation between)!

Re: Multiple assertions are fine in a unit test

#117
post #25
post #3

What? People really would criticize that code because it has two assertions? How are they ever testing any state changes? And to the author: Your bubble is significantly different from mine. Pretty much every competent developer I've worked with would laugh at you for the idea that the second test case would not be perfectly fine. (But that first iteration would never pass code review either because it does nothing a…

There's a lot of not very competent people in the industry who cling tightly to dogma. Testing (especially unit) is an area of tech weirdly with a lot of dogmatism. I think Uncle Bob is the source of some of it.

I'm convinced if you read Uncle Bob carefully and follow all his suggestions... you'll have completely incapacitated whatever organization you infiltrated.

Re: Multiple assertions are fine in a unit test

#118
post #4

Wait - people are doing real http calls in unit tests, over a network, and complaining about multiple asserts in the test code?

Hopefully the responses are stored locally and replayed on subsequent runs.

Still a bit flaky. In an OOP language mocking is appropriate or in an FP language defunctionalization.

Re: Multiple assertions are fine in a unit test

#119

The one assertion per test doesn't mean you need to use only one assertion call but rather that you only need to do one assertion block. Checking everything after a response is considered 1 assertion, no matter how many assert calls you need. The issue is when you use multiple assertions for multiple logic statements: do > assert > do > assert... In that example imagine that you were also checking that the reservatio…

Sometimes, it takes a lot less code to test a particular code by doing it in a long test with multiple assertions. Migrate up -> assert ok -> rollback 1 -> assert ok -> rollback 2 -> assert ok I don’t see much benefit to breaking it up, and you’re testing state changes between each transition, so the entire test is useful and simpler, shorter, and clearer than the alternative.

Sometimes it's also more meaningful to the reader this way.

Imagine an object which sees numbers, tries to pair up identical numbers, and reports the set of unpaired numbers. A good test would be:

Create object

Assert it has no unpaired numbers

Show it 1, 2, and 3

Assert it has 1, 2, and 3 as the unpaired numbers

Show it 1 and 3

Assert it has 2 as the unpaired number

This test directly illustrates how the state changes over time. You could split it into three tests, but then someone reading the tests would have to read all three and infer what is going on. I consider that strictly worse.

Re: Multiple assertions are fine in a unit test

#120
post #81

Things I find awkward with unit test (and it might just be me) is Inwant to write a test like def testLotsOfWaysTofail(): d = {"Handle Null": (None, foobar), "Don't allow under 13 to do it": (11, foobar), "or old age pensioner": (77,wobble)} for ... generate a unit test dynamically here I have built metaclasses, I have tried many different options. I am sure there is a near solution. But I never seem to have it right

isn't this just a parametrized test? or do you want to generate the test cases automatically like in property-based testing?

Yes parameterised testing. Just all the packages i have tried seemed to make it awkward to impossible - hence metaclasses
Post reply on HN