Generating py.test tests in Python

前端 未结 2 1556
攒了一身酷
攒了一身酷 2021-02-05 07:10

Question first, then an explanation if you\'re interested.

In the context of py.test, how do I generate a large set of test functions from a small set of test-function te

相关标签:
2条回答
  • 2021-02-05 07:50

    Good instincts. py.test supports exactly what you're talking about with its pytest_generate_tests() hook. They explain it here.

    0 讨论(0)
  • 2021-02-05 07:51

    You also could do that using parametrized fixtures. While hooks, is an API to build plugins for Py.test, parametrized fixtures is a generalized way to make a fixtures that outputs multiple values and generates additional test cases for them.

    Plugins are meant to be some project-wide (or package-wide) features, not test case specific features and parametrized fixtures are exactly what's needed to parametrize some resource for test case(s).

    So your solution could be rewritten as that:

    @pytest.fixture(params=[model1, model2, model3])
    def model(request):
        return request.param
    
    def test_awesome(model):
        assert model == "awesome"
    
    0 讨论(0)
提交回复
热议问题