Skip to content

Instantly share code, notes, and snippets.

@tdutrion
Last active February 17, 2017 16:27
Show Gist options
  • Save tdutrion/fe37bd77624b977f351ae9e84ebe26fd to your computer and use it in GitHub Desktop.
Save tdutrion/fe37bd77624b977f351ae9e84ebe26fd to your computer and use it in GitHub Desktop.
Unit testing - Nicolas

I don't have time to write tests because I am too busy

Types of software testing

Some people are confused about what type of tests they are practicing.

Explaination on what the different types of tests cover (acceptance => as a user)

Video of the doors.

Test pyramid, emphasising on costs of the tests, and therefore when it should be testing using which strategy.

For legacy rewriting, acceptance testing should probably be the best type of tests to choose, as it allows to confirm the code is iso in terms of features.

Writing tests

  • test use cases, not methods, and this should be visible by reading the test signature
  • tests can be documentation

A list of best practices in code writing is available in the slides.

A test is consistent, the execution is the same each time it runs. If not, that means that the test are worthless (meaning the positive result in CI does not guarantee a positive result on another environement).

Tests must be easy to read an provide the feature/intent at first sight. Naming is particularly important for tests as it is responsible in part for the self-desciption.

Tests must no contain any logic at all (loops or conditions).

PHPUnit assertions examples and malpractice

Choose assertions carefully (assertSame - === - is better than assertEquals - == in a strongly typed code).

Failed tests messages will be more descriptive if the right assertion is chosen.

Test doubles

Stub

Simulate the class to inject (ie: provide return value for each method of the interface/class).

Basically test whether with the same input we get the same output.

Mock

Just like a stub + expectation. Expectation would be testing the behaviour of the methods (ie. number of time it's called and so on).

Use expectations only when necessary, as expectations would need more maintenance time generally.

Code coverage

ADD AN EXAMPLE OF GOOD CODE COVERAGE WITH BAD TESTS

Good metric to see if paths are covered, but not the ultimate metric as some of the use cases may be left out.

PHPSpec

Alternative approach to PHPUnit, BDD oriented. It is an opinionated framework, so comes with limitations to guide developers to produce better tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment