Skip to content

An introduction to PyTest with lots of simple, hackable examples

Notifications You must be signed in to change notification settings

tenth10th/intro-to-pytest

 
 

Repository files navigation

intro-to-pytest

Python 2.7 Python 3.6 Python 3.7 Python 3.8 Python 3.9 Python 3.10 Python 3.11

(I have reluctantly forked this repo, as I no longer have access to maintain the original version - Please feel free to merge my recent changes back in, if you have the permission to do so!)

An introduction to PyTest with lots of simple, hackable examples, currently compatible with Python 2.7 and 3.6+ (up to 3.11).

These examples are intended to be self-explanatory to a Python developer, with minimal setup - In addition to your preferred version of Python, you'll also need pytest and the pytest-mock plugin installed to use all these examples, which you can install by running:

pip install -r requirements.txt

in this folder - ideally, inside a virtual environment, to keep this from affecting your local Python libraries. (Consider using venv or another virtual environment manager of your choice.)

Once you've got all the requirements in place, you should be able to simply run

pytest

In this folder, and see 77 items being collected, and 72 tests passing (with a few skipped or in other special statuses, which is intentional - more on that later...)

PyTest will list the names of each test module file that it found, and then a period for each test case that passed, or other symbols for tests that failed, were skipped, etc.

But if you're seeing all that, congratulations! You're ready to get started.

The recommended approach is to read each example file, then run it directly with pytest, with the v flag (so that each Test Case is listed "verbosely", by name) and the s flag, so that we can all the standard output (prints) from the Tests, which will help explain how each example is working; PyTest normally captures and hides this output, except for tests that are currently failing. (In the examples below, we'll shorten these arguements to -vs.)

Each example test was intended to be self-explanatory, but I have begun adding short tutorial guides to explain more of the context, suggest experiments and hacks you can attempt on th examples, and to provide recaps and reviews for each major section. The tutorial track starts with:

Tutorial Zero: An Empty Test

Not all of the examples have an accompanying tutorial (yet), but were written to be self-explanatory, and should at least include basic comments to explain the feature being demonstrated.

If you have any feedback, questions, or PyTest features you'd like to see covered, please let me know via GitHub Issues or a Pull Request. As I am no longer with Pluralsight, it has become somewhat more difficult to maintain this project = but since it is open source (and still seems popular) I am working with some former co-workers to keep it maintained. Thanks for your patience!

More information on how to test and develop in this project is now available in the Development doc.

About

An introduction to PyTest with lots of simple, hackable examples

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%