1

I have a requirement to implement a test suite for multiple functions. I am trying to figure out best practices to leverage existing pytest design pattern.

  • There are 2-3 common test cases for all the functions
  • Each function require different presetup condition

My current design :

 /utils
    logic.py
 /tests 
    Test_Regression.py
    Sedan/
        Test_Sedan.py
    SUV/
        Test_SUV.py
    Hatchback/
        Test_Hatchback.py
 /config
    Configuration.py

Current folder structure

  • Regression.py : This class holds common testcases

  • Test_SUV.py : This class inherits Test_Regression class test cases and has SUV specific test cases

  • Utils : This folder stores the program logic

is this a good design practice for a test suite to have class inheritance

   class Regression:
    @pytest.parameterize(x, utils.logic_func())
    @pytest.mark.testengine
    def test_engine(x,self):
        #validates logic
        assert x == 0
    @pytest.parameterize(y, utils.logic_func())    
    @pytest.mark.testheadlight
    def test_headlight(y,self):
        #validates logic
        assert y == 0
        
        
class Test_SUV(Test_Regression):
    
    def get_engine_values():
        # calls program logic
        return x
.
.
.
.

Or is there a better way to structure these test cases.

surabhi
  • 19
  • 4

1 Answers1

0
  1. Preconditions function can be annotated with @pytest.fixture and that can be used as parameter to the test methods instead of utility functions. You can define the scope (function, class, module, package or session) of these fixture functions: More details about fixture: https://docs.pytest.org/en/6.2.x/fixture.html
  2. Pytest over unittest - one reason is you can avoid implicit class requirement and keep your tests simple and less verbose. And if you are adding class for pytest, then we are losing this benefit. IMHO, you can keep your common tests in regression module and avoid Regression class and Test Class inheritance, because this is going to be hard to maintain in long run.
  3. Your tests should be independent from each other. If there is a common functionality that you want to share or inherit, you can do that via fixtures and keep them in a module called conftest.py and all those functions in conftest.py will then be available to all modules in the package and sub-packages More details about conftest
Felix K Jose
  • 782
  • 7
  • 10
  • Thanks for your response @felix, I have a follow up query reg point#2 if I remove the inheritance between the regression and SUV class then how can i have a single point of entry for my SUV testcases (Pardon my noob understanding). My current approach was to run the test_SUV.py file for the SUV test cases, as it is inheriting certain test functions from parent class, those will also be executed. – surabhi Jul 08 '21 at 22:28
  • @surabhi you can use `@pytest.mark.suv_test` for all your suv specific tests. And you want to run only suv related tests you can run them via: `pytest -m suv_test -s -vv`. But if you really want to do inheritance, go for it. In general, thats not a common approach, but each use case is different, so you could do what is convenient and maintainable to you. Since its a opinionated scenario. – Felix K Jose Jul 09 '21 at 13:30