Unit Testing in Python

Eugine Kang
Aug 25, 2017 · 1 min read

What are unit tests good for?

  1. Finding bugs DURING development
  2. Writing maintainable code
  3. Documenting developer’s intention

What type of test cases should be generated?

  1. Possible inputs
  2. Categorize inputs into special cases
  3. One test per special case

Example) Palindrome

How would you structure your unit test?

  1. Define your inputs and any preconditions
  2. Verify expected output

Common test cases for function converting a number

  1. Negative number
  2. Zero
  3. Fraction
  4. Positive number
  5. Large number, > 1000
  6. Wrong data types
  7. NaN

How to use pytest fixtures?

Define shortcut to custom subclass functions

How to run multiple values with one test?

Note to self

  1. Assume certain conditions are met for individual functions
  2. However, test for wrong input when testing entire process
  3. Build functions on what you can test
  4. Test for ideal cases and calculate expected outcome
  5. Test so that each line of code is being executed as expected, if-else statements

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade