Test to Code

·

2 min read

The major benefit of testing happens when you think about tests and write them, not when you run them. Thinking about writing tests forces the developer to write testable code. Testable code is code that respects the development design principles. Because of this tests should drive coding.

While writing the test, the developer is forced to look at the code from the outside, as if it were a client of the code (in a matter of contracts, invariants, inputs and outputs), not the author.

A test is the first user of your code.

Test-Driven Development

Unit testing

Reasons:

  • we have confidence that individual parts work as expected

  • we get a better feeling of how units of code interact with other units of code once it is tested in isolation

  • we can also test the code with an integration test, but it would be harder to debug exactly which part of the code within the integration test failed.

  • it encourages the thinking that we are testing against the contract (whether the code meets the contract, and whether the contract means what we think it means)

Regression Testing

They check the returned results of the test against known results or results from previous runs of the same test.

Property-based Testing

  • they help you find bad assumptions about the code you wrote and missing edge cases.