Timeline for Is it OK to have multiple asserts in a single unit test?
Current License: CC BY-SA 3.0
26 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 15, 2023 at 7:39 | comment | added | TheEvilMetal | Definitely not the case. I'd say 1 set of asserts per test case. But for tests that require a lot of setup I'm basically putting the entire test into a separate method, then calling it for a million specific tests. May as well just have a theory test with different sets of data making up the test cases. And in the case of a failing test I'd be investigating what made it fail. I don't need visibility of all asserts separately because once there's a failing test I'll be investigating what to do to fix it anyway. | |
| May 10, 2022 at 7:07 | comment | added | gnasher729 | @Artur Maybe it's not? Maybe someone is just doing extensive testing? | |
| Mar 19, 2021 at 16:03 | comment | added | Artur INTECH | @tom Maybe the class itself is too big then? | |
| Nov 9, 2020 at 8:56 | comment | added | Lee Benson | Disagree with this. If the code leading to an assertion is elaborate, it makes little sense to repeat that for the sake of perceived single-assertion purity. Perfectly acceptable to have multiple assertions in a test if they're all related. | |
| Oct 6, 2020 at 23:31 | comment | added | OSGI Java | You did not explain why we should strive towards only having single asserts. | |
| Oct 28, 2019 at 14:08 | comment | added | ToastyMallows | @JacoPretorius ah my HTTPS Everywhere extension was forcing the link to HTTPS, which is a 404. HTTP works just fine. | |
| Oct 25, 2019 at 13:21 | comment | added | Jaco Pretorius | @ToastyMallows The link works fine for me? owenpellegrin.com/blog/testing/… | |
| Oct 24, 2019 at 12:52 | comment | added | ToastyMallows | Link returns a 404 | |
| Jan 29, 2019 at 17:09 | comment | added | dtc | woah, it's a code smell to have 5 or more asserts? definitely not and depends on the situation. | |
| Jun 2, 2016 at 12:18 | comment | added | Peter | Strongly disagree. The answer doesn't list any advantage of having single assert, and the obvious disadvantage is that you need to copy-paste tests only because an answer on the internet says so. If you need to test multiple fields of a result or multiple results of a single operation, you absolutely should assert all of them in independent asserts, because it gives far more useful information than testing them in a big blob-assert. In a good test, you test a single operation, not a single result of an operation. | |
| Dec 22, 2015 at 13:36 | comment | added | Steven Jeuris | Any reasoning behind this? As is, this current answer just states what it should be, but not why. | |
| Jul 17, 2015 at 11:30 | comment | added | pvinis | a case where you need multiple assertions for me is when i need to test math/geometry functions. i add assertions for the basic cases, corner cases, and then a couple random ones. so if i need to test some point conversion between views, for example, i will add the points to the corners of the view, a point inside the view, and a point outside the view. | |
| May 12, 2015 at 13:03 | history | edited | user40980 | CC BY-SA 3.0 | Remove meta lead in. Write out small numbers (see http://english.stackexchange.com/q/979/40348 ) |
| Apr 8, 2014 at 23:27 | comment | added | Byron Ross | @jgauffin It depends whether you are using unit test to mechanically execute the code or to define the expected behavior. If you take the behavior approach (which I prefer) you end up with 2 tests. | |
| Apr 8, 2014 at 23:25 | comment | added | Byron Ross | @Tom, I have as a requirement of my test runners that they link to the code - e.g. If i double click on a failed test it should take me straight to the code file. Console runners obviously are more difficult! | |
| Nov 6, 2012 at 23:17 | comment | added | Thomas Eding | bad thing strive towards only having single asserts code (test?) smell | |
| Oct 30, 2012 at 15:45 | comment | added | Tom | A single assert per unit test is a great way to test the reader's ability to scroll up and down. | |
| Oct 30, 2012 at 15:08 | comment | added | jgauffin | Ehh? Why would you do that? the method execution is exactly the same? | |
| Oct 30, 2012 at 15:03 | comment | added | MattDavey | @jgauffin wouldn't those be seperate tests though? eg. SomeMethod_Alters_FooProperty(), SomeMethod_Alters_BarProperty() etc. The unit tests would be identical apart from the assertion. | |
| Oct 30, 2012 at 14:00 | comment | added | jgauffin | Passing an argument into a single argument method can result in several properties being changed of that object. Hence a simple operation can require multiple asserts. Take Stream.Write | |
| Oct 26, 2012 at 13:53 | comment | added | CaffGeek | I do it a bit. For example, if I'm testing comparability, and that ItemA > ItemB I'll also assert that ItemB < ItemA in the same test. | |
| Oct 1, 2010 at 10:42 | vote | accept | Restuta | ||
| Sep 29, 2010 at 10:04 | vote | accept | Restuta | ||
| Sep 29, 2010 at 10:04 | |||||
| Sep 28, 2010 at 11:45 | comment | added | Murph | Like this answer - i.e. its OK, but its not, in the general instance, good (-: | |
| Sep 28, 2010 at 11:28 | history | edited | Jaco Pretorius | CC BY-SA 2.5 | added 132 characters in body |
| Sep 28, 2010 at 11:22 | history | answered | Jaco Pretorius | CC BY-SA 2.5 |