代码之家  ›  专栏  ›  技术社区  ›  garg10may

将夹具信息传递给正常功能

  •  0
  • garg10may  · 技术社区  · 3 年前

    我如何将特定固定装置中的信息传递给正常函数,即它不是以 test_ ?

    import pytest
    
    @pytest.fixture
    def my_fixture():
        return 'Hello world!'
    
    def test_one(my_fixture):
        assert my_fixture == 'Hello world!'
    

    由于该函数以 测试_ 。我想要下面这样的东西:

    @pytest.mark.usefixtures("my_fixture")
    def my_example():
        print(my_fixture())
    

    或者如果我可以将其用作变量,如:

    a = pytest.get_fixture("my_fixture")
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Hai Vu    3 年前

    如果可以,请创建一个函数,然后从fixture调用该函数:

    import pytest
    
    
    def hello():
        return "hello"
    
    
    @pytest.fixture
    def my_fixture():
        return hello()
    
    
    def test_direct(my_fixture):
        # Use fixture directly
        assert my_fixture == "hello"
    
    
    def test_indirect():
        # Cannot call a fixture, but can call function that make up the fixture
        assert hello() == "hello"