如何在unittest中参数化测试用例和设置

yvfmudvl  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(198)

每个人我有两个装置和两个测试用例。下面是我将它们粘在一起的想法(以避免编写4个单独的测试用例):

from unittest import TestCase

class TypeAFixture:
    def setUp(self):
        print("\nSetting up type A")

class TypeBFixture:
    def setUp(self):
        print("\nSetting up type B")

class Scenario:
    def test_case_1(self):
        print("testing Case 1")

    def test_case_2(self):
        print("testing Case 2")

class TestA(TypeAFixture, Scenario, TestCase):
    pass

class TestB(TypeBFixture, Scenario, TestCase):
    pass

按照预期工作,具有以下输出(我使用pytest运行它):

main.py::TestA::test_case_1 
Setting up type A
testing Case 1
PASSED
main.py::TestA::test_case_2 
Setting up type A
testing Case 2
PASSED
main.py::TestB::test_case_1 
Setting up type B
testing Case 1
PASSED
main.py::TestB::test_case_2 
Setting up type B
testing Case 2
PASSED

问题是-我需要对此进行扩展,并对这两种设置实施额外的参数化。我尝试过不同的方法(使用子测试,为装置提供额外的参数等等),但到目前为止似乎没有任何效果。我期待以下输出:

main.py::TestA::test_case_1 
Setting up type A
Setting up param X
testing Case 1
PASSED
main.py::TestA::test_case_2 
Setting up type A
Setting up param Y
testing Case 2
PASSED
main.py::TestB::test_case_1 
Setting up type B
Setting up param X
testing Case 1
PASSED
main.py::TestB::test_case_2 
Setting up type B
Setting up param Y
testing Case 2
PASSED

正确的方法是什么?如有任何建议,将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题