Nino's Blog

Testing best pratices

2019-25-12T21:09:31.324Z

Testing best pratices

List of testing best pratices:

  • Test like user would use system, don’t test implementation details, like redux, react libs
  • Use mostly integration tests
  • Have at least one e2e happy path test
  • Use unit tests mostly for edge cases and pure functions
  • Test things where you need confidence
  • Mock network calls in unit tests, expect in e2e tests

    • In unit testing mock DB, but integration testing use real DB
  • Don’t strive for code coverage

    • When your done with integration tests and few e2e, you can improve coverage with units tests. Focus on users use cases, not code coverage
  • You should be able to run all your tests in parallel

    • Do teardown even if test break.
  • Test entire flow with multiple expect is better then testing single piece of flow in isolation

    • It makes testing easier, makes error messages in test easier.
  • Test should be simple and clear to understand
  • Does test name has 3 part (what, condition, result)

    • What is being tested? For example, the ProductsService.addNewProduct method
    • Under what circumstances and scenario? For example, no price is passed to the method
    • What is the expected result? For example, the new product is not approved
  • Stick to black-box testing: Test only public methods

    • Before using test doubles, ask a very simple question: Do I use it to test functionality that appears, or could appear, in the requirements document? If no, it’s a smell of white-box testing.
  • Use realistic input data (no “foo”)
  • Utilize property-based testing (all the possible input combinations)
  • Avoid global test fixtures and seeds, add data per-test

    • In reality, this is often violated by testers who seed the DB with data before running the tests (also known as ‘test fixture’) for the sake of performance improvement.
  • Don’t catch errors, expect them

    • In Jest: expect(method).toThrow().

Sources:


Written by Nino Majder who lives and breaths web development. Follow him on Twitter