| 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 pytest |
| 12 |
from _hypothesis_pytestplugin import PRINT_STATISTICS_OPTION |
| 13 |
|
| 14 |
from tests.common.utils import skipif_threading |
| 15 |
|
| 16 |
pytest_plugins = "pytester" |
| 17 |
|
| 18 |
|
| 19 |
def get_output(testdir, suite, *args): |
| 20 |
script = testdir.makepyfile(suite) |
| 21 |
result = testdir.runpytest(script, *args) |
| 22 |
return "\n".join(result.stdout.lines) |
| 23 |
|
| 24 |
|
| 25 |
TESTSUITE = """ |
| 26 |
from hypothesis import HealthCheck, given, settings, assume |
| 27 |
from hypothesis.strategies import integers |
| 28 |
import time |
| 29 |
import warnings |
| 30 |
from hypothesis.errors import HypothesisDeprecationWarning |
| 31 |
|
| 32 |
warnings.simplefilter('always', HypothesisDeprecationWarning) |
| 33 |
|
| 34 |
|
| 35 |
@given(integers()) |
| 36 |
def test_all_valid(x): |
| 37 |
pass |
| 38 |
|
| 39 |
|
| 40 |
@settings(max_examples=100, suppress_health_check=list(HealthCheck)) |
| 41 |
@given(integers()) |
| 42 |
def test_iterations(x): |
| 43 |
assume(x == 13) |
| 44 |
""" |
| 45 |
|
| 46 |
|
| 47 |
def test_does_not_run_statistics_by_default(testdir): |
| 48 |
out = get_output(testdir, TESTSUITE) |
| 49 |
assert "Hypothesis Statistics" not in out |
| 50 |
|
| 51 |
|
| 52 |
def test_prints_statistics_given_option(testdir): |
| 53 |
out = get_output(testdir, TESTSUITE, PRINT_STATISTICS_OPTION) |
| 54 |
assert "Hypothesis Statistics" in out |
| 55 |
assert "max_examples=100" in out |
| 56 |
assert "< 1% of examples satisfied assumptions" in out |
| 57 |
|
| 58 |
|
| 59 |
def test_prints_statistics_given_option_under_xdist(testdir): |
| 60 |
out = get_output(testdir, TESTSUITE, PRINT_STATISTICS_OPTION, "-n", "2") |
| 61 |
assert "Hypothesis Statistics" in out |
| 62 |
assert "max_examples=100" in out |
| 63 |
assert "< 1% of examples satisfied assumptions" in out |
| 64 |
|
| 65 |
|
| 66 |
def test_prints_statistics_given_option_with_junitxml(testdir): |
| 67 |
out = get_output(testdir, TESTSUITE, PRINT_STATISTICS_OPTION, "--junit-xml=out.xml") |
| 68 |
assert "Hypothesis Statistics" in out |
| 69 |
assert "max_examples=100" in out |
| 70 |
assert "< 1% of examples satisfied assumptions" in out |
| 71 |
|
| 72 |
|
| 73 |
@skipif_threading |
| 74 |
@pytest.mark.skipif( |
| 75 |
tuple(map(int, pytest.__version__.split(".")[:2])) < (5, 4), reason="too old" |
| 76 |
) |
| 77 |
def test_prints_statistics_given_option_under_xdist_with_junitxml(testdir): |
| 78 |
out = get_output( |
| 79 |
testdir, TESTSUITE, PRINT_STATISTICS_OPTION, "-n", "2", "--junit-xml=out.xml" |
| 80 |
) |
| 81 |
assert "Hypothesis Statistics" in out |
| 82 |
assert "max_examples=100" in out |
| 83 |
assert "< 1% of examples satisfied assumptions" in out |
| 84 |
|
| 85 |
|
| 86 |
UNITTEST_TESTSUITE = """ |
| 87 |
|
| 88 |
from hypothesis import given |
| 89 |
from hypothesis.strategies import integers |
| 90 |
from unittest import TestCase |
| 91 |
|
| 92 |
|
| 93 |
class TestStuff(TestCase): |
| 94 |
@given(integers()) |
| 95 |
def test_all_valid(self, x): |
| 96 |
pass |
| 97 |
""" |
| 98 |
|
| 99 |
|
| 100 |
def test_prints_statistics_for_unittest_tests(testdir): |
| 101 |
script = testdir.makepyfile(UNITTEST_TESTSUITE) |
| 102 |
result = testdir.runpytest(script, PRINT_STATISTICS_OPTION) |
| 103 |
out = "\n".join(result.stdout.lines) |
| 104 |
assert "Hypothesis Statistics" in out |
| 105 |
assert "TestStuff::test_all_valid" in out |
| 106 |
assert "max_examples=100" in out |
| 107 |
|
| 108 |
|
| 109 |
STATEFUL_TESTSUITE = """ |
| 110 |
from hypothesis.stateful import RuleBasedStateMachine, rule |
| 111 |
|
| 112 |
class Stuff(RuleBasedStateMachine): |
| 113 |
@rule() |
| 114 |
def step(self): |
| 115 |
pass |
| 116 |
|
| 117 |
TestStuff = Stuff.TestCase |
| 118 |
""" |
| 119 |
|
| 120 |
|
| 121 |
def test_prints_statistics_for_stateful_tests(testdir): |
| 122 |
script = testdir.makepyfile(STATEFUL_TESTSUITE) |
| 123 |
result = testdir.runpytest(script, PRINT_STATISTICS_OPTION) |
| 124 |
out = "\n".join(result.stdout.lines) |
| 125 |
assert "Hypothesis Statistics" in out |
| 126 |
assert "TestStuff::runTest" in out |
| 127 |
assert "max_examples=100" in out |