| 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 inspect |
| 12 |
from unittest import TestCase |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import example, given |
| 17 |
from hypothesis.strategies import booleans, integers |
| 18 |
|
| 19 |
|
| 20 |
def test_must_use_result_of_test(): |
| 21 |
class DoubleRun: |
| 22 |
def execute_example(self, function): |
| 23 |
x = function() |
| 24 |
if inspect.isfunction(x): |
| 25 |
return x() |
| 26 |
|
| 27 |
@given(booleans()) |
| 28 |
def boom(self, b): |
| 29 |
def f(): |
| 30 |
raise ValueError |
| 31 |
|
| 32 |
return f |
| 33 |
|
| 34 |
with pytest.raises(ValueError): |
| 35 |
DoubleRun().boom() |
| 36 |
|
| 37 |
|
| 38 |
class TestTryReallyHard(TestCase): |
| 39 |
@given(integers()) |
| 40 |
def test_something(self, i): |
| 41 |
pass |
| 42 |
|
| 43 |
def execute_example(self, f): |
| 44 |
f() |
| 45 |
return f() |
| 46 |
|
| 47 |
|
| 48 |
class Valueless: |
| 49 |
def execute_example(self, f): |
| 50 |
try: |
| 51 |
return f() |
| 52 |
except ValueError: |
| 53 |
return None |
| 54 |
|
| 55 |
@given(integers()) |
| 56 |
@example(1) |
| 57 |
def test_no_boom_on_example(self, x): |
| 58 |
raise ValueError |
| 59 |
|
| 60 |
@given(integers()) |
| 61 |
def test_no_boom(self, x): |
| 62 |
raise ValueError |
| 63 |
|
| 64 |
@given(integers()) |
| 65 |
def test_boom(self, x): |
| 66 |
raise AssertionError |
| 67 |
|
| 68 |
|
| 69 |
def test_boom(): |
| 70 |
with pytest.raises(AssertionError): |
| 71 |
Valueless().test_boom() |
| 72 |
|
| 73 |
|
| 74 |
def test_no_boom(): |
| 75 |
Valueless().test_no_boom() |
| 76 |
|
| 77 |
|
| 78 |
def test_no_boom_on_example(): |
| 79 |
Valueless().test_no_boom_on_example() |