码迷,mamicode.com
首页 > 其他好文 > 详细

pytest-fixture

时间:2019-07-23 00:00:31      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:默认   als   conf   文件中   com   on()   scope   assert   func   

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 启动目录   会启动一个本地的服务

pytest-fixture

标签:默认   als   conf   文件中   com   on()   scope   assert   func   

原文地址:https://www.cnblogs.com/an5456/p/11229218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!