Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

python - Pytest run entire test suite with a selectable set-up fixture

I'd like to essentially "inherit" a pytest suite to test different implementations of a library using the same set of test functions. I'm struggling to find answers in Google and Stack Overflow, but I think a large part of my problem is that I don't know the correct pytest nomenclature for what I want.

I'm working on a library which contains multiple implementations of an interface. Let's call them A and B.

I've got a reasonably large test suite developed against implementation A and now I want to also run the same suite against B avoiding code duplication.

I've got a fixture for each implementation that spins up the necessary resources for a test instance of A and B.

@pytest.fixture(scope='function')
def get_an_A():
    yield A(..)

And a fixture for B that depends on some other 3rd party fixtures.

@pytest.fixture(scope='function')
def get_a_B(some_other_fixture):
    yield B(..)

And a test suite that's happy to run against_either

def test_add_values_should_be_correct(my_A_or_B):
    assert my_A_or_B.of(1) + my_A_or_B.of(2) == my_A_or_B.of(3)

What I'd like to do is construct that my_A_or_B fixture that can provide As using the A fixture in one context and Bs using the B fixture in another context.

Ideally I'd like to be able to run something like this:

Run test suite against A:

> pytest tests/A

Run test suite against B:

> pytest tests/B

Run test suite against A then B

> pytest tests/

Is there a way of laying out my test suite that will allow me to do this?

question from:https://stackoverflow.com/questions/65938977/pytest-run-entire-test-suite-with-a-selectable-set-up-fixture

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the easiest way is have all the tests in the same folder and run this (on both file's names and method's names, names must start with test_ prefix)

python -m unittest discover <PATH_TO_THE_FOLDER>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...