mirror of
https://github.com/jbranchaud/til
synced 2026-07-03 08:08:24 +00:00
Add Skip Specific Pytest Test Cases as a Python TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1768 TILs and counting..._
|
||||
_1769 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1061,6 +1061,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Look Inside Pytest tmp_path](python/look-inside-pytest-tmp-path.md)
|
||||
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
||||
- [Parse Relative Time To datetime Object](python/parse-relative-time-to-datetime-object.md)
|
||||
- [Skip Specific Pytest Test Cases](python/skip-specific-pytest-test-cases.md)
|
||||
- [Start The Debugger When A Test Errors](python/start-the-debugger-when-a-test-errors.md)
|
||||
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
|
||||
- [Test A Function With Pytest](python/test-a-function-with-pytest.md)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# Skip Specific Pytest Test Cases
|
||||
|
||||
While using a failing test case to build a small new feature for
|
||||
[`py-vmt`](https://github.com/jbranchaud/py-vmt), I realized I needed to do some
|
||||
refactoring first. It wasn't significant enough to warrant stashing my current
|
||||
changes and switching to a different branch, so I kept all the changes around. I
|
||||
did find the initial failing test distracting from the refactoring I was trying
|
||||
to do. To temporarily shelve that failure, I can use a Pytest decorator to mark
|
||||
it as _skipped_.
|
||||
|
||||
```python
|
||||
@pytest.mark.skip(reason="not yet implemented")
|
||||
def test_log_recent_activity():
|
||||
runner = CliRunner()
|
||||
|
||||
# set up the data dir file with some existing session entries
|
||||
initial_datetime = datetime.datetime(
|
||||
2026, 3, 14, 15, 5, 11, 0, datetime.timezone.utc
|
||||
)
|
||||
with freeze_time(initial_datetime) as frozen_datetime:
|
||||
# ...
|
||||
```
|
||||
|
||||
The [`@pytest.mark.skip` decorator](https://docs.pytest.org/en/stable/how-to/skipping.html#skipping-test-functions)
|
||||
tells the Pytest runner to skip of that specific test case instead of executing
|
||||
it. In the test runner output, I'll see an `s` rather than a `.` or `F` and the
|
||||
summary will include it in a count of skipped tests:
|
||||
|
||||
```
|
||||
=========================== 3 failed, 4 passed, 1 skipped in 0.09s ===========================
|
||||
```
|
||||
|
||||
Another way to think about this is to mark this test case as _expected to fail_
|
||||
with `@pytest.mark.xfail`. That will display as an `x` and show up in the summary as:
|
||||
|
||||
```
|
||||
=========================== 3 failed, 4 passed, 1 xfailed in 0.11s ===========================
|
||||
```
|
||||
Reference in New Issue
Block a user