<commit_before>---
topic: "Python: unittest"
desc: "module for test-driven development in Python"
---

# Using `unittest` to test your Python code

Test-driven development involves writing "unit tests" for each individual
unit of code: typically each function definition.

Unit tests, or "test cases" as they are sometimes called, are specified
by:

* Writing a function call with an actual parameter (the actual result)
* Writing the value what we expect that function to return (the expected results)
* *Asserting* that those are equal to each other, i.e. `actual==expected`

Then, if `actual==expected`, the test cases passes, otherwise it fails.

In the case of tests involving decimals, we might instead assert that
the actual and expected are *almost equal*, meaning that the absolute
value of the difference between them is less than some small
value&mdash;or to say it another way, that they are equal within a
certain number of decimal places.

One way to write these test cases in Python is with a module that we can
import called `unittest`.  

As an example, consider this simple function that converts Fahrenheit
to Celsius, stored in `tempFuncs.py`<commit_msg>Update unittest.md<commit_after>---
topic: "Python: unittest"
desc: "module for test-driven development in Python"
---

This page describes using `unittest` to test Python code.

If you are new to Python, you may want to first consider looking at either:
* [testing](/ptopics/testing): notes on simple approaches to interactive testing
* [pytest](/ptopics/pytest): a alternative to `unittest` that may students and instructors find easier to understand, but that requires a separate installation through `pip install --user pytest`

# Using `unittest` to test your Python code

Test-driven development involves writing "unit tests" for each individual
unit of code: typically each function definition.

Unit tests, or "test cases" as they are sometimes called, are specified
by:

* Writing a function call with an actual parameter (the actual result)
* Writing the value what we expect that function to return (the expected results)
* *Asserting* that those are equal to each other, i.e. `actual==expected`

Then, if `actual==expected`, the test cases passes, otherwise it fails.

In the case of tests involving decimals, we might instead assert that
the actual and expected are *almost equal*, meaning that the absolute
value of the difference between them is less than some small
value&mdash;or to say it another way, that they are equal within a
certain number of decimal places.

One way to write these test cases in Python is with a module that we can
import called `unittest`.  

As an example, consider this simple function that converts Fahrenheit
to Celsius, stored in `tempFuncs.py`
{'fraction_non_alphanumeric': 0.06109906001446132, 'fraction_numerical': 0.0, 'mean_word_length': 4.752598752598753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2806933', 'n_tokens_mistral': 698, 'n_tokens_neox': 688, 'n_words': 409}