Some Basic PyTesting
- Bence Danko
- Jul 26, 2021
- 1 min read

Beyond print statements, I've never really dealt with testing in depth so far in my academic learning. However, I've come to learn that frequent testing is an excellent practice to get used to, and there are some programs that are especially designed to help, such as PyTest.
I've set up a basic program that determines the average, median and mode of a list. After installing PyTest in my environment, I've set up some tests for my functions like so:
def test_average():
assert find_average([1, 3, 4, 5]) == 3.25
def test_median():
assert find_median([1, 3, 4, 5]) == 3.5
def test_mode():
assert find_mode([1, 3, 3, 4]) == 3 With assert, I can test whether these functions produce the results I expect. In order to utilize PyTest, I called on PyTest in PowerShell using:
$ pytest test.pyTo specify running a single test function, I was able to use:
$ pytest test.py -k <test_function> -vWhich ignored the other test functions and only gave me the results more my median method.
I am aware that there are some more complex things you can do with PyTest, so I might make explore more of its capabilities in the future.



Comments