[Python] Pytest equivalent for unittest-style base/derived class tests

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I have an older Python test suite that used a unittest class-based setup for testing, and I'm trying to convert it over to use pytest. An example of which is shown below where a base class with generalized tests is defined and then derived classes set class attributes that are then used by the tests. These are pretty large classes with many tests and many class attributes that control the tests for the derived classes. My main questions are:

    1.) Should classes be used at all with pytest?

    2.) Is there a particular pattern for this? i.e. "When you see base/derived tests, think of this pytest equivalent approach"

    class Base:
    val_1 = None
    val_2 = None
    val_3 = None
    val_4 = 5
    val_5 = 0

    def test_1(self):
    def test_2(self):


    class Test1(Base):
    val_1 = 1
    val_2 = 2
    val_3 = 3

    class Test2(Base):
    val_1 = 2
    val_2 = 3
    val_3 = 4

    class Base2(Base):
    val_4 = 0
    val_5 = 0

    def test_3(self):
    def test_4(self):

    Class Test3(Base2):
    val_1 = 1
    val_2 = 2
    val_3 = 3

    Continue reading...

Compartilhe esta Página