Earlier quoted context omitted.
If you’re using pytest you just paramaterize the tests and it tells you the exact failing case. Seems to be a basic feature I would be surprised to know doesn’t exist across almost all commonly used frameworks.
We are using unittest. But thanks for bringing it up, it seems it also has "subtest" support, which might be easier than interpolating the error message in some cases: https://docs.python.org/3/library/unittest.html#distinguishi...
Multiple assertions are fine in a unit test
101–110 of 348 posts
Re: Multiple assertions are fine in a unit test
#102Re: Multiple assertions are fine in a unit test
#103Earlier quoted context omitted.
I remember writing a small .NET test library for that exact problem - You could pass in a lambda with a complex condition, and it evaluated every piece of the expression separately and pretty printed what part of the condition failed. So essentially you could write Assert(()=>width>0 && x + width And you would get: Assertion failed: x is 1500 width is 600 screenWidth is 1920 It used Expression to do the magic. Amazin…
Is this shared somewhere?
Re: Multiple assertions are fine in a unit test
#104Earlier quoted context omitted.
OP asked how any state change would be tested with a single 'assertion' and I provided an answer. Absolute rules are stupid, but our codebase has just short of 10k tests, and very few have more than one assertion. The only reason I can really see to have more than one assertion would be to avoid having to run the setup/teardown multiple times. However, its usually a desirable goal to write code that require little se…
one assert per test seems... as you said, indicative of zealotry. if you already have your object there, why not test for the changes you expect? So you have one test that indicates that a log error is outut. then another that tests that the property X in the return from the error is what you expect. then another test to determine that propery Y in return is what you expect? that to me is wasteful, unclear, bloated.…
That's one way to get dogma-driven assertion roulette, as you will not know which particular error occurred.
Re: Multiple assertions are fine in a unit test
#105Re: Multiple assertions are fine in a unit test
#106Test setup is sometimes very complicated AND expensive. Enforcing only 1 assertion per test is moronic beyond description.
Re: Multiple assertions are fine in a unit test
#107Earlier quoted context omitted.
That’s because the example test only requires 1 assertion. Any rule that says there should be only 1 assertion ever is stupid.
OP asked how any state change would be tested with a single 'assertion' and I provided an answer. Absolute rules are stupid, but our codebase has just short of 10k tests, and very few have more than one assertion. The only reason I can really see to have more than one assertion would be to avoid having to run the setup/teardown multiple times. However, its usually a desirable goal to write code that require little se…
Re: Multiple assertions are fine in a unit test
#108Earlier quoted context omitted.
> How is that even possible in the first place? The entire job of an assertion is to wave a flag saying "here! condition failed!". I envy you for never having seen tests atrocious enough where this is not only possible, but the common case. Depending on language, framework and obviously usage, assertions might not be as informative as providing the basic functionality of failing the test - and that's it. Now imagine…
So because some idiot somewhere wrote a 100 assertion unit test we should ban anyone from writing even 2 assertions in one test?
Re: Multiple assertions are fine in a unit test
#109Earlier quoted context omitted.
In my experience, from doing some TDD Katas[0] and timing myself, I found coding slower and more difficult when focusing on multiple examples at once. I usually even comment out all the failing tests but the first one, after translating a bunch of specifications into tests, so I see the "green" when an example starts working. Maybe it would be easier to grok multiple regex examples than algorithmic ones, but at least…
Again pytest makes things so much nicer in this regard. Having to comment things out sucks. With pytest you can use the -x flag to stop after the first test failure. Even better you can use that in combination with -lf to only run the last failed test.
> Even better you can use that in combination with -lf to only run the last failed test.
Fwiw `--sw` is much better for that specific use-case.
`--lf` is more useful to run the entire test suite, then re-run just the failed tests (of the entire suite). IIRC it can have some odd interactions with `-x` or `--maxfail`, because the strange things happen to the cached "selected set".
Though it may also be because I use xdist a fair bit, and the interaction of xdist with early interruptions (x, maxfail, ...) seems less than perfect.
Re: Multiple assertions are fine in a unit test
#110Wait - people are doing real http calls in unit tests, over a network, and complaining about multiple asserts in the test code?
- "But it tests more things!"
Well ok, but those are integration tests, not unit tests... It is unacceptable that a unit tests can fail because of external system...