Skip to content

Instantly share code, notes, and snippets.

@eddiecorrigall
Last active July 31, 2017 15:26
Show Gist options
  • Save eddiecorrigall/0040e5a723a217e016f580dd9fb641e9 to your computer and use it in GitHub Desktop.
Save eddiecorrigall/0040e5a723a217e016f580dd9fb641e9 to your computer and use it in GitHub Desktop.
Unit Test Ideas

Unit test ideas

Deterministic VS Stocastic

Unit tests should always be deterministic / reproducable. It is hard to debug when tests might fail randomly.

  • Assign a seed to the RNG

Avoid conditional-statements

Unit tests should always follow the same execution path, and hit all asserts regardless of the global or local state. This ensures consistent behaviour and that there is no assumptions on whether conditional control flows have been accurately written.

Create fixtures from database not datasets

Although it is important that datasets are tested on some level, its more important to test what will be used in production: ORM entities. Assume that there can be inconsistencies in the process of transforming a dataset into the database.

  • Representation of a dataset might change, but its less likely that the database entity representation will change
  • Datasets might not translate exactly to a database entity, ie. datasets might not have a primary key

Test what is assumed to be consistent

  • Always have tests for the interface. Tests for helper functions arent as useful unless they are used to generate the data for the interface directly.
  • Determine the source of truth and write tests around that

Use code to describe behaviour

password_hash = generate_password_hash(password='password1')

VS

# the password is `password1`
password_hash = 'a_long_complex_hash_string'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment