I had a test suite with thousands of tests. One way of running it was to take all the passing tests, and then run them repeatedly in random order.<p>This found new bugs involving unintended persistent state.
After a bunch of experiences like this I will now basically only use test runners that automatically run the tests in randomized order.<p>Gotta catch that stuff early.
Shout out to AVA for running all tests async by default, which not only finds order-dependent issues, but also race condition and other timing related issues.<p><a href="https://github.com/avajs/ava" rel="nofollow">https://github.com/avajs/ava</a>
Reversing the test order is also pretty effective: <a href="https://pypi.org/project/pytest-reverse/" rel="nofollow">https://pypi.org/project/pytest-reverse/</a>
While I don't doubt the veracity of your report, I don't think this is an efficient testing strategy.<p>Ideally, you don't want a very large number of tests, no matter how big the system is. Tests are, effectively, an interface to the program that assesses the system quality / readiness for use. Any interface with thousands of individual pieces is difficult to use.<p>Random combinations of tests also don't spark joy because this means both repetition (i.e. waste of resources) and testing potentially useless (unreachable or invalid) system states (both wastes resources and creates false alarms).<p>Ideally, the tests should be able to compose only in desired ways and rather than combining them randomly, there should be some deterministic process that creates a unique sequence or a tree of individual tests on subsequent runs. Ideally, such a test runner could also be configured to start with an existing system in a known state s.t. the tester can apply a patch and resume testing.
Someone had written a comment about whether Kent Beck has heard of global and static variables. That comment seems to have been deleted.<p>Kent Beck is co-creator [1] of the JUnit Testing Framework and has most definitely heard of static and global variables.<p>[1] <a href="https://junit.org/junit4/project-info.html" rel="nofollow">https://junit.org/junit4/project-info.html</a>
Trimming the copy-paste chain is right. The pieces being green isn’t the same as the app opening.
Ah Kent Beck, I see your style of writing trivial examples hasn't changed
I'm done listening to smart people proposing fixes for my dumb code.<p>Either I'm not smart or disciplined enough to make it work, or my colleagues are not. Mostly both.
Kent Beck is smart because he proposes fixes that work in real life, as in environments with dumb code and undisciplined people.<p>I can very much recommend his latest book ”Tidy first?”. It’s extremely short and concise, and is perfect for a very light book club within any tech team.
I assure you, there is no such thing as a book club at my job, especially not one focused on technical writing :)
I agree, both with your analysis of him and your review of "Tidy first?". We read and discussed it at work, and I enjoyed it a lot.
I enjoyed the last (and theoretical) part of "Tidy first?" as I was already doing the recommended practices in the first two. But yes, very pragmatic advice and easy to apply to get your own corner of peace in a somewhat chaotic codebase.<p>One term I can use is "defensive programming" (not sure if the term has been used before). People are going to do awful stuff with their code, it's up to you to draw boundary of correctness to stop it from spilling on the part that you're responsible for. It should be automated (with tests and static analysis) as well as documented. I think of those boundary as hazmat suits and I take extra care of maintaining their integrity.
What he wrote is basically "don't repeat in test X what you already tested in test X-1".<p>It's not as much composition as compounding, and can work quite well.<p>Let's say something takes 20 steps, and you want to test all 20.<p>Instead of this:<p><pre><code> test 1:
do step 1, assert step 1
test 2:
do step 1, assert step 1
do step 2, assert step 2
test 3:
do step 1, assert step 1
do step 2, assert step 2
do step 3, assert step 3
...
</code></pre>
you do this:<p><pre><code> test 1:
do step 1, assert step 1
test 2:
do step 1
do step 2, assert step 2
test 3:
do step 1
do step 2
do step 3, assert step 3
...
</code></pre>
This works well in certain situations, as it skips diplicated redundant testing. Requires some discipline so that tests don't drift away from each other.<p>As most things, it depends on what those steps are. Perhaps you only need that one final (integration) test instead of 20 intermediate unit ones.
I wonder if it would work do design something that was able to say<p><pre><code> test 1:
do step 1, assert step 1
test 2:
requires: test 1
do step 2, assert step 2
test 3:
requires: test 2
do step 3, assert step 3
test 4:
requires: test 2
do step 3, assert step 3
</code></pre>
If the assertion of each test doesn't change any state, that might make things easier to read. Though, given that I haven't spent much time pondering it, I expect it could have it's own problems.<p>But it could also do things like skip test 3 if tests 2 or 1 failed - because it knows about the relationship.
The pattern can work, but the domain matters, the test type matters (unit, integration, ui) and the trade-off associated matter.<p>e.g. If I am running a long-running UI-test scenario, I absolutely don't want test-5 to walk through 80% of the UI that was already exercised in tests 1-4. I am creating test coupling, but I'm saving cost/time by doing so.<p>But, you'll also hear why not to do this, because it creates test coupling / breaks atomic tests, which is generally seen as bad.<p>If that is a local integration test and those early steps run is millis? Then maybe we keep things uncoupled to allow the system to exercise the pathways without explicit expectations.
The hard parts to design about this, imo, are:<p>- How to reconcile this with tests that execute many times with varying input data. You’d need some way to express requirements with specific inputs or shared inputs.<p>- Passing state between test dependencies.<p>- When, if ever, it’s fine to share step results between tests. If tests B and C require A, can you run A just once? Not always, but you should be able to when it’s safe.<p>I don’t think I’ve ever used a test framework that gets these things right.
Yes that is the way to do it, in Spock that’s what @Stepwise does.
This make too much sense!<p>In special when testing against a DB.
Yup. I'd love to have dependency declarations for tests like this.
That's... A thing in many frameworks. If it's not in yours, you could add it.<p>Forgive an old man some ruby:<p><pre><code> it 'relies on mobile setup defined elsewhere', :mobile => true do
# test that relies on mobile setup here
end</code></pre>
[flagged]
What's the problem of only keeping test 3 if it depends on test 2 and 1 passing anyway?<p>The article mentions not to do this because "Deleting test1 loses us another property from the Test Desiderata—tests should be specific. That’s the property of tests where, when one fails, you know exactly where the problem is." but you'll know what line it failed on. And some test runners let you break a test into steps, where groups of lines are given a description.<p>Or put each step + assert in a helper function (e.g. `doStep1AndAssert()`), and each test only calls these helper functions?<p>Nothing is perfect, but copy/pasting chunks between tests like this isn't great when you want to refactor and it's repetitive to read.
> What's the problem of only keeping test 3 if it depends on test 2 and 1 passing anyway?<p>That depends.<p>Sometimes <i>test 1</i> tests a combination of ways (e.g., property testing, or just going through a bunch of various inputs), and only a few of those are needed for <i>test 2</i>.<p>Sometimes you don't want your <i>test 2</i> to be more complicated than it already is. Or the same things are needed checked in other tests. So you extract them into <i>test 1</i>.<p>And sometimes (and in some of code bases <i>most</i> of the time) <i>test 1</i> is redundant and unnecessary. That's why I always advocate investing in integration tests (test 20) and skip all the intermediate tests.
IMO it seems kind of terrible.<p>You're not going to remember what tests "test 3" relies on. They aren't actually linear progressions 123. They will be "test this", "test that". If 3 fails you're going to want to immediately go and add all those asserts back to help you debug your assumptions.<p>The tests _will_ drift and that should be fine. Implicitly depending on other tests doesn't really get me anything.
I think I disagree with Kent, but your explanation is clearer, so I'll object here.<p>There's nothing wrong with hitting the same assertion multiple times, even if it doesn't sit nicely in your gut.<p>From a purely philosophical point of view: If I have testFoo(), testBar(), and testFooAndBar(), and my Foo is plain wrong, then both testFoo() and testFooAndBar() must fail. Anything less is misleading/dishonest.<p>From a practical side: Changes happen. Someone will remove testBar(), and then you're down to 0 assertions on Bar, even though you have a test claiming to testFooAndBar(). It's not even a crazy hypothetical. Someone with a different test philosophy will think (to quote TFA) "They are redundant! Something must be wrong." and delete testBar() because obviously testFooAndBar() already covers it.<p>Anyway, we all know how to deal with repetition. That's what programming is!<p><pre><code> testFoo()
_ = validateFoo(foo())
testBar()
_ = validateBar(bar())
testFooAndBar()
foo = validateFoo(foo())
bar = validateBar(bar())
_ = validateFooAndBar(fooAndBar(foo, bar))</code></pre>
What I do not like about that kind of example is its abstractiveness. Yes sure you can argue about testing `doSomething()` and it all falls apart when there is an actual business scenario to test.
It's not hard, with some modest experience (say 1-2 years of professional work or serious amateur interest), to extrapolate from his deliberately high-level discussion (it was written for a blog/newsletter, not for a book) to something more interesting and "real world". What's hard is coming up with an example that's nearly complete and fits into something the size of a newsletter or comment. He did an alright job of it, just left the readers with the need to exercise their gray matter a bit.
1. I'm not sure what CodeRabbit has to do with the article... It's obviously an advertisement, but it's merged into the text of the article, which I find bizarre. As an aside, I used CodeRabbit at work, and I have mixed feelings about it. I'm not entirely against using it or a similar tool, but people often treat the comments from CodeRabbit as a gospel, and they can harm their code as a result. Also, I've never seen CodeRabbit being anything more than a superficial reviewer: fixing typos, other unintended errors, but it never comments on the substance of the change, which implicitly validates it for the author.<p>2. On test composition. Unit tests are called "unit" because they are supposed to test one thing. If a test is testing more than one thing, it's an integration test. Ideally, people writing unit tests are the developers themselves and people writing integration tests are test (automation) people. This matters for administrative reasons: in the development cycle, the unit test is the basic check that validates a particular piece of code, probably, submitted for review or for merging. Passing such a test might be a necessary condition to progress the changeset along the designed workflow path. Integration tests, on the other hand, are more of a retrospective tool that is meant for detection of problems in the entire product. A failure of an integration test should, normally, schedule new task for the developers, not reject the one being worked on. Integration tests, typically, will require a more elaborate system under test setup and a more elaborate, perhaps involving multiple teams, investigation of the failure. They can be also a lot more expensive to run in terms of equipment used.<p>Finally, the author touched on a contentious subject a.k.a. the number of assertions in a test. A lot of people believe (me included) that the number of assertions <i>in the unit test</i> should be exactly one. This is often inconvenient because it requires implementation of equality for possibly ad hoc created set of results. Even so, I believe it's still worth it.<p>When it comes to integration tests, I don't believe assertions are <i>at all</i> the way to go. The system under test should be monitored continuously and every reading should be compared against the desired state of the system that the test modifies simultaneously with the change effected to the system. This is because, in practice, it's rarely just two features that are tested together. If the test waits until the final step to compare the desired and the actual state of the system, the error as well as the context in which it happened might be long gone.<p>As a side bonus: the monitoring+alerts system could well be part of the product itself, or, if not, it can be used in long-running tests intended to collect mileage (i.e. tests intended to prove that the system performance doesn't degrade over substantially long periods of time).
Ehhh... when it truly is two tests bodged into one, then sure.<p>But sometimes you do this kind of thing to avoid useless test brittleness: does your test check that `doSomething()` does what you expect, or do you have another test for that and this test <i>only</i> checks that `nowSomethingElse()` changes the object in a predictable way, e.g. updates a calculated field?<p>If it's the former, then it might be two tests masquerading as one, and this might make sense.<p>If it's the latter, you've changed a test that only checks what it cares about, and now you have a test that depends on unrelated implementation detail and will probably break unnecessarily in the future. Plus you've removed the assert that was documenting what it expects, so it's harder to tell if fixing it <i>should</i> mean updating both checks, or only the second one.
> If a test runs by first setting up its own test fixture, creating from scratch all the data it will be using as input, then that test is guaranteed to be isolated. It doesn’t matter what order you run the tests, the results will be exactly the same.<p>Completely backward. This definition <i>requires</i> isolation, rather than <i>granting</i> it.<p>In reality, you (or your test framework) <i>provides</i> the isolation by hobbling along, executing only one test at a time.
> Deleting test1 loses us another property from the Test Desiderata—tests should be specific. That’s the property of tests where, when one fails, you know exactly where the problem is.<p>Contra Kent and, it seems, prevailing wisdom, I think simply deleting test1 is by far the simplest, clearest and best way. Provided that your testing framework tells you which specific assertion failed (e.g., by telling you the line number in a stack trace), you <i>do</i> know exactly where the problem is. The only thing you lose is that a test function or method may now cover several related checks (they are related by "setup dependence"), meaning their names may need to be somewhat broader. But you can still describe the specific semantics of each assert() check in a one-line comment beforehand if you want. There's no need to cram it into a legal method name.<p>ETA: Prefer to write tests whose "arrange" steps are as simple as possible, to minimise unnecessary overlaps. But if the simplest possible "arrange" step for a test is something that itself needs to be checked for correctness, <i>just do that check right there, and nowhere else</i>. Anything beyond that is ceremony that adds nothing useful.
I think this could lead to the tests getting harder to reason about over time because individual test size will just grow (many iterations of deleting the simple test in favor of a more complicated test that also asserts the simple things)<p>and if you start simplifying the more complicated test later, you may not realize that you're accidentally deleting checks that don't exist anywhere else other than incidentally here. So it's less clear what's important without thinking it through each time.<p>now all that to say I don't think it's strictly that big of a deal either way - there's always tons of room for taste that can make one or the other way better in practice. But that was my gut-reaction when reading your comment.
> Contra Kent and, it seems, prevailing wisdom, I think simply deleting test1 is by far the simplest, clearest and best way. Provided that your testing framework tells you which specific assertion failed (e.g., by telling you the line number in a stack trace), you do know exactly where the problem is.<p>The issue with your approach is that codepath execution is more of a graph than a linear timeline. With one test, you artificially constrains it to a linear timeline even if the assertions are correct. With multiples and independent you only assert one specific node. That lets you switch up how you do the preliminary steps. I much prefer an exhaustive test unit for step 1, and just a regular call in step 2 and step 3.
If you need test1's logic as setup for test2, then you already have that "linear timeline" -- for test2 all by itself. Additionally (that is, redundantly) running the same setup code "prefix" by itself in test1 doesn't remove test2's linear timeline.<p>ETA: I'm assuming your objection to a "linear timeline" is that it reduces the potential for running tests in parallel -- have I got that right? If not, what do you see as being the problem with it?
If I know that step 1 is correct for all the combinations (N) of its input (in test 1), in test 2, I only need to test the specific combinations (M) of step 2, treating step 1 as an axiom. So no need to have NxM in one test, or NxM tests.<p>Like if you were testing a drone stabilization software, testing the flying state can always assume that it has indeed taken off. No need to validate that it has done so for each scenarios, I can directly put it the correct value. It’s a contrived example, but that axiomatic aspect helps greatly when designing interfaces to reduce coupling between step 1 and step 2.
[flagged]