TIL how to re-run only failed tests
⋅ 1 minute read
This is a super small thing I stumbled over. When I implement a new feature, there is inevitably a step where I go through the failing pytest tests and fix or clarify them.
Unless I use an IDE, my work loop is like this:
- Make code change
- Run
python -m pytest - Check which tests are failing.
- Go back to 1.
Now, 2. runs all tests which can take quite some time. There is always the option to run a specific test only, but multiple failing tests can be scattered across different files.
So instead, what you can do is to only run the tests that failed at your last test run. Simply use:
python -m pytest --last-failed
Once you have fixed those tests, it’s worth running all tests one more time to check that nothing else was broken by the new changes.
Another option is to use the test view of your IDE, if you have access to one in that environment.
If you have any thoughts, questions, or feedback about this post, I would love to hear it. Please reach out to me via email.
Tags:#python