How to pass environment variables to pytest

后端 未结 8 734
小鲜肉
小鲜肉 2020-12-05 01:48

Before I start executing the tests in my Python project, I read some environment variables and set some variables with these values read. My tests will run on the desired en

8条回答
  •  爱一瞬间的悲伤
    2020-12-05 01:58

    In addition to other answers. There is an option to overwrite pytest_generate_tests in conftest.py and set ENV variables there.

    For example, add following into conftest.py:

    import os
    
    def pytest_generate_tests(metafunc):
        os.environ['TEST_NAME'] = 'My super test name| Python version {}'.format(python_version)
    

    This code will allow you to grab TEST_NAME ENV variable in your tests application. Also you could make a fixture:

    import os
    import pytest
    
    @pytest.fixture
    def the_name():
        return os.environ.get('TEST_NAME')
    

    Also, this ENV variable will be available in your application.

提交回复
热议问题