pytest-fixture

fixtture使用:
如何两个方法都是用同一个数据,则就可以使用fixture
    def test_1():
    json = requests.get("https://testerhome.com/api/v3/topics.json?limit=2").json()
    assert len(topics['topics']) == 2

    def test_2():
        json = requests.get("https://testerhome.com/api/v3/topics.json?limit=2").json()
        assert topics['topics'][0]['deleted'] == False
    俩个方法都用到json的数据,就可以使用fixture
    @pytest.fixture()
    def topics():
    url = 'https://testerhome.com/api/v3/topics.json?limit=2'
    return requests.get(url).json()
    def test_1(topics):
        assert len(topics['topics']) == 2

    def test_2(topics):
        assert topics['topics'][0]['deleted'] == False

    @pytest.fixture(scope="function")
    def topics():
    url = 'https://testerhome.com/api/v3/topics.json?limit=2'
    return requests.get(url).json()
    当scope="function"时,也是默认的,在每个方法前都会执行;
    当scope="module"时,每个模块都会执行一次
    当scope="session"时,所有的模块都只执行一次

    也可以将fixture的方法定义到conftest.py文件中,会自动读取
    python3 -m http.server 启动目录   会启动一个本地的服务

猜你喜欢

转载自www.cnblogs.com/an5456/p/11229218.html