by ysavir · 2y ago · 0 comments
The purpose of unit testing is to isolate the various scenarios by making sure that each unit/component/whatever correctly responds to its various inputs. In this way, we only need to set up the relevant scenario for that unit. If we tested each of these piece of functionality in an E2E test, then each test would also need to set up the user session, settings, etc. It would be a nightmare to author and maintain, compared to the simple, straight forward unit tests.
In contrast, E2E tests are good at making sure that the whole system works well together, and that each unit is getting inputs and providing outputs, and that the page doesn't break in the process. Both are needed to maintain confidence, and each solves a different problem.
If your E2E test is flaky, then how is your production environment reliable?
> they can require a lot of setup
Setup vs mock.
> they can be very slow, etc.
We have a good handle on scaling, slow is now a product of the testing time being smeared across your org like peanut butter.
> The purpose of unit testing...
Does E2E replace unit testing, no. Things that are already units or esoteric (business "logic", you know it's in your code base). But if your writing mocks your better off un fucking your environment that E2E testing is viable, and make it a first class citizen. Everything is a crud app of some sort: web, api, mobile, smoke signal... Dropping data in these front ends, can be validated out of the back end, and in the app itself.
To put a fine point on it: Unit tests make me feel good about checking in. End to end tests make me confident to deploy!
IME, E2E tests are flaky despite the underlying system not being flaky because E2E has to do a series of things, often including waiting/polling, and in real life people will wait longer or compensate but in tests you want to do it all perfectly and fast.
From an end-user's perspective, there's very little difference between "the task didn't work" and "the task worked, but took longer than your patience to wait." Both appear to be broken.
The flakiness comes from needing an automated browser interaction acting against a page with changing input. If your page is static, sure, the E2E test is probably straightforward and quick. But the moment any dynamic functionality is introduced to the page, the test's interactions need to account for the page's current context (or wait for an intended context). And that has to be coordinated amongst the actual test code, the testing browser, the driver, etc. If anyone one of them hiccups, you may end with a false negative, and those happen more often than we'd like.
I disagree. Assume it usually takes 1sec but 1% of the time it takes 120sec. Does your test suite wait 120sec and fail 1% of the time or does it wait 1sec and speed up the test suite? These are test trade-offs IMHO
Whether you use playwright or cypress getting the correct wait events figured out is a huge pain.
Only if you run pure server side applications that’s simple.
End to End testing is less about "making the front end do all the work" and more about being free of mocks and synthetics.
Just because the front end exercises my server, does not mean server devs get a pass on testing API down. The tests I write should inform the data your sending and receiving, they should function as an example of inputs and outputs of what to expect in the system. Those timing issues are either a real front end problem, or a server problem (and my own tests should have noted that I did not meet the SLA).
That really took the value of E2E down several notches in my estimation.
Same for any testing. it should be part of the cost and Cost Benefit Analysis.
Right now I'm still trying to convince people that fakes and mocks are not the same thing.
A good end to end test suite will give you a much truer indication of the state of your systems.
Of course I don’t say do zero unit tests, they have value, but I think too many orgs are too zealous about unit tests and too soft on higher level testing.