Skip to content

Commit

Permalink
Merge pull request #10 from ionelmc/master
Browse files Browse the repository at this point in the history
Remove extra function call from context manger API
  • Loading branch information
okken authored Feb 24, 2019
2 parents 2efd253 + 064fb73 commit 5ff8d8e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ from pytest_check import check


def test_context_manager():
with check():
with check:
x = 3
assert 1 < x < 4
```

Within any `with check():`, however, you still won't get past the assert statement,
so you will need to use multiple `with check():` blocks for multiple asserts:
Within any `with check:`, however, you still won't get past the assert statement,
so you will need to use multiple `with check:` blocks for multiple asserts:

```python
def test_multiple_failures():
with check(): assert 1 == 0
with check(): assert 1 > 2
with check(): assert 1 < 5 < 4
with check: assert 1 == 0
with check: assert 1 > 2
with check: assert 1 < 5 < 4

```

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from io import open
from os import path
from setuptools import setup, find_packages

Expand Down
29 changes: 16 additions & 13 deletions src/pytest_check/check_methods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from contextlib import contextmanager
import functools
import inspect
import os
Expand Down Expand Up @@ -45,17 +44,21 @@ def set_stop_on_fail(stop_on_fail):
_stop_on_fail = stop_on_fail


@contextmanager
def check():
__tracebackhide__ = True
try:
yield
except AssertionError as e:
if _stop_on_fail:
raise e
log_failure(e)
finally:
pass
class CheckContextManager(object):
def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
__tracebackhide__ = True
if exc_type is not None and issubclass(exc_type, AssertionError):
if _stop_on_fail:
return
else:
log_failure(exc_val)
return True


check = CheckContextManager()


def check_func(func):
Expand Down Expand Up @@ -178,7 +181,7 @@ def log_failure(msg):
if 'contextlib.py' in file:
pass
else:
line = f" {file}, line {line}, in {func}() -> {context}"
line = " {}, line {}, in {}() -> {}".format(file, line, func, context)
pseudo_trace.append(line)
level += 1
pseudo_trace_str = "\n".join(reversed(pseudo_trace))
Expand Down
14 changes: 7 additions & 7 deletions tests/test_check_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# flake8: noqa

def test_context_manager():
with check():
with check:
x = 3
assert 1 < x < 4

Expand All @@ -17,9 +17,9 @@ def test_context_manager_fail(testdir):
from pytest_check import check
def test_failures():
with check(): assert 1 == 0
with check(): assert 1 > 2
with check(): assert 1 < 5 < 4
with check: assert 1 == 0
with check: assert 1 > 2
with check: assert 1 < 5 < 4
"""
)

Expand All @@ -40,9 +40,9 @@ def test_stop_on_fail(testdir):
from pytest_check import check
def test_failures():
with check(): assert 1 == 0
with check(): assert 1 > 2
with check(): assert 1 < 5 < 4
with check: assert 1 == 0
with check: assert 1 > 2
with check: assert 1 < 5 < 4
"""
)

Expand Down

0 comments on commit 5ff8d8e

Please sign in to comment.