Skip to content
This repository has been archived by the owner on Mar 11, 2023. It is now read-only.

Identifing inner test functions from pytest fixture or pytest hooks #3

Closed
mitzkia opened this issue Oct 13, 2019 · 1 comment
Closed

Comments

@mitzkia
Copy link

mitzkia commented Oct 13, 2019

Hi,

I found your project via: HypothesisWorks/hypothesis#377
I think it is easier to describe my question through an example :) .
Let assume following files.

  • conftest.py
def pytest_runtest_setup(item):
    print("\nMessage from pytest setup, pytest item name: [%s]" % item.name)
  • test_example.py
from hypothesis import given, settings
from hypothesis.strategies import integers

@settings(max_examples=2,)
@given(integers())
def test_main(subtest, hypothesis_value):
    print("\nMessage from main testcase, test value: [%s]" % hypothesis_value)
    @subtest
    def test_concrete():
        print("\nMessage from concrete testcase, test_value: [%s]" % hypothesis_value)

When I run my test it will produce the following STDOUT output:

...
test_example.py::test_main 
Message from pytest setup, pytest item name: [test_main]
Message from main testcase, test value: [0]
Message from pytest setup, pytest item name: [test_main[]]
Message from concrete testcase, test_value: [0]
Message from main testcase, test value: [193]
Message from pytest setup, pytest item name: [test_main[]]
Message from concrete testcase, test_value: [193]

In my usecase I would like to run pytest hooks (or fixtures) only for "test_concrete" (or inner) like functions.
From pytest_runtest_setup "test_concrete" function is identified as "test_main" + "[]".
It means I should test (in pytest_runtest_setup) if my item.name contains: "[]" or not, which is not so informative.

My question:
Could we identify "test_concrete" (or inner) like functions better than: "my main function" + "[]"

Thank you very much

@mitzkia mitzkia changed the title Identifing subtest from pytest fixture or pytest hooks Identifing inner test functions from pytest fixture or pytest hooks Oct 13, 2019
@mitzkia
Copy link
Author

mitzkia commented Oct 13, 2019

Ok, after think over this case, it is not so critical :) . I also can make an informative convention in my code:

def is_run_for_inner_test(item):
    return "[]" in item.name
def pytest_runtest_setup(item):
    if is_run_for_inner_test(item):
        do_my_stuff()

So thanks for this plugin, it saves my use-case :)

@mitzkia mitzkia closed this as completed Oct 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant