| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import unittest |
| 12 |
|
| 13 |
import pytest |
| 14 |
from _pytest.outcomes import Skipped |
| 15 |
|
| 16 |
from hypothesis import given, settings |
| 17 |
from hypothesis.core import skip_exceptions_to_reraise |
| 18 |
from hypothesis.database import InMemoryExampleDatabase |
| 19 |
from hypothesis.strategies import integers |
| 20 |
|
| 21 |
from tests.common.utils import capture_out |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize("skip_exception", skip_exceptions_to_reraise()) |
| 25 |
def test_no_falsifying_example_if_unittest_skip(skip_exception): |
| 26 |
"""If a ``SkipTest`` exception is raised during a test, Hypothesis should |
| 27 |
not continue running the test and shrink process, nor should it print |
| 28 |
anything about falsifying examples.""" |
| 29 |
|
| 30 |
class DemoTest(unittest.TestCase): |
| 31 |
@given(xs=integers()) |
| 32 |
def test_to_be_skipped(self, xs): |
| 33 |
if xs == 0: |
| 34 |
raise skip_exception |
| 35 |
else: |
| 36 |
assert xs == 0 |
| 37 |
|
| 38 |
with capture_out() as o: |
| 39 |
suite = unittest.defaultTestLoader.loadTestsFromTestCase(DemoTest) |
| 40 |
unittest.TextTestRunner().run(suite) |
| 41 |
|
| 42 |
assert "Falsifying example" not in o.getvalue() |
| 43 |
|
| 44 |
|
| 45 |
def test_skip_exceptions_save_database_entries(): |
| 46 |
"""Skip exceptions should save database entries for immediate replay (issue #4484).""" |
| 47 |
database = InMemoryExampleDatabase() |
| 48 |
call_count = 0 |
| 49 |
skip_value = None |
| 50 |
first_value = None |
| 51 |
|
| 52 |
@settings(database=database, max_examples=100) |
| 53 |
@given(integers()) |
| 54 |
def test_func(n): |
| 55 |
nonlocal call_count, skip_value, first_value |
| 56 |
call_count += 1 |
| 57 |
if first_value is None: |
| 58 |
first_value = n |
| 59 |
|
| 60 |
# Skip on the 5th value in the first run (the choice of 5 is arbitrary) |
| 61 |
if call_count == 5 and skip_value is None: |
| 62 |
skip_value = n |
| 63 |
|
| 64 |
if n == skip_value: |
| 65 |
pytest.skip() |
| 66 |
|
| 67 |
# First run should raise a skip exception and save to database |
| 68 |
with pytest.raises(Skipped): |
| 69 |
test_func() |
| 70 |
assert sum(len(v) for v in database.data.values()) == 1 |
| 71 |
|
| 72 |
# Second run should immediately replay the skip value |
| 73 |
first_value = None |
| 74 |
call_count = 0 |
| 75 |
with pytest.raises(Skipped): |
| 76 |
test_func() |
| 77 |
|
| 78 |
# first call should be the replayed skip value |
| 79 |
assert first_value == skip_value |
| 80 |
assert call_count == 1 |