Create and import helper functions in tests without creating packages in test directory using py.test

后端 未结 7 606
北恋
北恋 2020-12-24 04:57

Question

How can I import helper functions in test files without creating packages in the test directory?


Contex

7条回答
  •  醉梦人生
    2020-12-24 05:16

    You could define a helper class in conftest.py, then create a fixture that returns that class (or an instance of it, depending on what you need).

    import pytest
    
    
    class Helpers:
        @staticmethod
        def help_me():
            return "no"
    
    
    @pytest.fixture
    def helpers():
        return Helpers
    

    Then in your tests, you can use the fixture:

    def test_with_help(helpers):
        helpers.help_me()
    

提交回复
热议问题