2

I am using environment variables to control some Hypothesis settings in conftest.py:

test_max_examples = int(os.environ.get("TEST_MAX_EXAMPLES", "100").strip())
hypothesis.settings.register_profile(
    "dev",
    print_blob=True,
    max_examples=test_max_examples,
)

hypothesis.settings.load_profile("dev")

I would like to print these settings for the user's benefit, before running the tests.

However, my naive attempt failed, because Pytest captures output:

print(hypothesis.settings.get_profile("dev"))

I am aware that the capsys fixture can be used inside tests to bypass output capturing, but I don't believe fixtures can be used at the top level of conftest.py.

Is there another solution to this? Or is this some kind of antipattern, and I'm not able to do this for a good reason?

I do not want to require the use of -s in Pytest unconditionally.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96

1 Answers1

1

The print should work for you if you use a fixture in conftest.py to set the settings. Use scope='session', autouse=True to run it once automatically

@pytest.fixture(scope='session', autouse=True)
def set_hypothesis():
    test_max_examples = int(os.environ.get("TEST_MAX_EXAMPLES", "100").strip())
    hypothesis.settings.register_profile(
        "dev",
        print_blob=True,
        max_examples=test_max_examples,
    )
    
    hypothesis.settings.load_profile("dev")
    print(hypothesis.settings.get_profile("dev"))
Guy
  • 46,488
  • 10
  • 44
  • 88