PyTest Usage

12 Apr 2020

This week I’ve gotten back into my course on web development with Python and have been building an API using API Star. I had a quick teachable moment after I finished writing the API, write the first test function and ran the test file for the first time.

The test file ran successfully, but no info about test outcomes appeared in the terminal window. I tried again a couple of times, and was really scratching my head about why I wasn’t seeing the output I was expecting. I had run tests in pytest when I last worked on this topic a few weeks ago, and I’ve been testing extensively with unittest more recently. What could be the issue now?

After a minute or so puzzling over this, I ran a search for ‘running tests pytest’ and pulled up the pytest Usage & Invocations page and quickly found my error. To run tests in a module with pytest, like I was doing, one runs pytest <test_mod.py> I had been running python3 <test_mod.py> because this is how it’s done in unittest.

I ran the test file again with the correct usage, and the tests ran successfully, outputing expected info on test results.

A few reminders & learning I gained from this experience:

#! TODO: link to API repo / course page #! TODO: link to pytest page Pytest usage: https://docs.pytest.org/en/latest/usage.html