pytest小例子

通过这两天学习的这点东西,写了个简单的案例:

# conftest.py
import pytest
import time
from selenium import webdriver


@pytest.fixture(scope='session')
def driver(request):
    driver = webdriver.Chrome()
    def end():
        driver.quit()
    request.addfinalizer(end)
    return driver

@pytest.fixture()
def start(driver):
    print('打开百度')
    driver.get('https://www.baidu.com')
    time.sleep(5)

# test_class.py
import pytest
import time


def test_1(driver,start):
    print('搜索python')
    driver.find_element_by_id('kw').send_keys('python')
    driver.find_element_by_id('su').click()
    time.sleep(5)
def test_2(driver,start):
    print('搜索地图')
    driver.find_element_by_id('kw').send_keys('地图')
    driver.find_element_by_id('su').click()
    time.sleep(5)

if __name__ == '__main__':
    pytest.main(['-q', 'test_class.py'])

# test_classs.py
import pytest


class Testclass:

    def test_1(self,driver):
        driver.get('https://www.baidu.com')
        print('打开百度')
    def test_2(self,driver):
        driver.get('https://i.qq.com/')
        print('打开空间')
if __name__ == '__main__':
    pytest.main(['-q', 'test_classs.py'])

上边脚本思路:在每条case前都将浏览器切回百度首页进行操作),然后我们切换到case目录下用命令运行,通过运行脚本我们发现,全程只打开了一次浏览器,进行了4次操作,然后关闭!这点要比unittest方便很多,不会像unittest一样,每次都会启动一个浏览器

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值