Unit Testing in Python
Aug 25, 2017 · 1 min read
What are unit tests good for?
- Finding bugs DURING development
- Writing maintainable code
- Documenting developer’s intention
What type of test cases should be generated?
- Possible inputs
- Categorize inputs into special cases
- One test per special case
Example) Palindrome
How would you structure your unit test?
- Define your inputs and any preconditions
- Verify expected output
Common test cases for function converting a number
- Negative number
- Zero
- Fraction
- Positive number
- Large number, > 1000
- Wrong data types
- NaN
How to use pytest fixtures?
Define shortcut to custom subclass functions
How to run multiple values with one test?
Note to self
- Assume certain conditions are met for individual functions
- However, test for wrong input when testing entire process
- Build functions on what you can test
- Test for ideal cases and calculate expected outcome
- Test so that each line of code is being executed as expected, if-else statements