Back to the basics: Tests, part 6 (The last one!)
After a long journey about tests, the tools available, strategies and what to avoid, let's see good practices! How to write and maintain a good tests code base.
- Clear and Concise: The test must be simple and easily to understand that the goal of the test and what it is testing.
- Repeatable: I can run the same test a hundred time, and it must always return the same assertions for the same input.
- Comprehensive: Must be simple to see all the things that the test uses, the AAA pattern is a good example to make clear the steps.
- Independent: You must be capable of running any test individually, cannot have coupling between tests.
- Validated: Well, it must validate something.
- Traceable: If you are testing a method that fail, the return must be traceable to point to the original implementation, for you found the code easily.
- Maintainable: Again, tests are code, so remember, if you create test for all your code, you will have two code bases at the end, so treat them equally.
So, let's see some good techniques to create tests in a better way.
- Test Driven Development (TDD): TDD is a famous design technique that involves write the test before the code, this practice guarantees great tests as you think your use case first and a better code quality that obligate you write a maintainable code with a good separation of concerns.
- Behavior Driven Development (BDD): More commonly used in end-to-end tests, is a great way to think about the existing flows in the application, bring the security that the "happy path" (the flow expected that the users does) are happening.
- Error Guessing: Basically is make some type of predict about what the things can be wrong in the code, and create tests scenarios for it, like someone try to inject a SQL code into the user form, things like that.
- New bug, new test: A excellent practice, if something breaks at the production because some user make a different flow or use some invalid character, when you fix, already add a test case for these scenarios and try to guess new ones from this experience.
- Exploratory Testing: This is really the famous joke for the QA that came to the bar and ask for 99999 beers, "XXX" beers, -7777 beers, etc. It is always nice to add some edges cases and stress out the logic a little bit.
So, let's write some tests! After this big journey you have a lot of material to start write tests now into your applications, to see a lot of the techniques applied in a project, I will recommend you to see a repository of a Node.js training that I gave some time ago, here is the spec file.
That's it for today!