zoukankan      html  css  js  c++  java
  • pytest学习conftest.py配置

    # conftest.py配置

      当多个用例调用一个模块的功能时,比如:如果有多个.py的文件都需要调用这个登陆功能的话,那就不能把登陆写到用例里面去了。
    此时应该要有一个配置文件,单独管理一些预置的操作场景,pytest里面默认读取conftest.py里面的配置

    conftest.py配置需要注意以下点:

    • conftest.py配置脚本名称是固定的,不能改名称
    • conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
    • 不需要import导入 conftest.py,pytest用例会自动查找
    • 单独运行test_fixt.py能调用到mylogin()方法,这样就能实现一些公共的操作可以单独拿出来了
    # 代码结构:

    __init__
    .py conftest.py # coding:utf-8 import pytest @pytest.fixture() def login(): print("输入账号/密码登录") test_f1.py # coding:utf-8 import pytest def test_s1(login): print("用例1") def test_s2(): print("用例2") def test_s3(login): print("用例3") if __name__ == "__main__": pytest.main(["-s", "test_f1.py"]) test_f2.py # coding:utf-8 import pytest def test_s4(login): print("用例4") def test_s5(): print("用例5") if __name__ == "__main__": pytest.main(["-s", "test_f2.py"])

     ❤项目实例:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2021/1/21 14:31
    # @Author  : Administrator
    # @File    : conftest.py
    import time
    
    import pytest
    from selenium import webdriver
    from py_test_pack.test_data.locator_eles import Mylocator
    
    @pytest.fixture()
    def login():
        driver = webdriver.Chrome()
        driver.get("http://192.168.0.xxx")
        driver.implicitly_wait(10)
        driver.maximize_window()
        loc = Mylocator
        uname = loc.username_loc
        username = driver.find_element_by_css_selector(uname)
        username.send_keys("yourname")
    
        pwd = loc.password_loc
        password = driver.find_element_by_css_selector(pwd)
        password.send_keys("yourpassword")
    
        subbutton = loc.login_button_loc
        login_button = driver.find_element_by_css_selector(subbutton)
        login_button.click()
        yield driver
        driver.quit()
        return driver

    # scope="module"   

      1.fixture参数scope="module",module作用是整个.py文件都会生效,用例调用时,参数写上函数名称就行
      2.module级别的fixture在当前.py模块里,只会在用例(test_s2)第一次调用前执行一次。

    import pytest
    from selenium import webdriver
    
    
    @pytest.fixture(scope="module")
    def open_broswer():
        driver = webdriver.Chrome()
        driver.get("http://www.baidu.com")
    
    
    def test_s1(open_broswer):
        print("用例1")
    
    
    def test_s2(open_broswer):
        print("用例2")
    
    
    if __name__ == '__main__':
        pytest.main(['-s', "test_f1.py"])

    当只有test_s2调用open时,顺序会发生什么样的变化?

    import pytest
    from selenium import webdriver
    
    
    @pytest.fixture(scope="module")
    def open_broswer():
        driver = webdriver.Chrome()
        driver.get("http://www.baidu.com")
        print("打开浏览器")
    
    
    def test_s1():
        print("用例1")
    
    
    def test_s2(open_broswer):
        print("用例2")
    
    
    def test_s3():
        print("用例3")
    
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_f1.py"])

    yield 执行teardown

      1.如果其中一个用例出现异常,不影响yield后面的teardown执行,运行结果互不影响,并且全部用例执行完之后,yield呼唤teardown操作
      2.如果在setup就异常了,那么是不会去执行yield后面的teardown内容了
      3.yield也可以配合with语句使用

    import pytest
    
    
    @pytest.fixture(scope="module")
    def open():
        print("打开浏览器")
    
        yield
        print("执行teardown")
        print("关闭浏览器")
    
    
    def test_s1(open):
        print("用例1")
    
    
    def test_s2(open):
        print("用例2")
        raise NameError  # 模拟异常
    
    
    def test_s3(open):
        print("用例2")

     ♥项目实战

    # 封装的driver驱动
    
    driver = None
    
    @pytest.fixture(scope='session', autouse=True)
    def browser(request):
        global driver
        if driver is None:
            driver = webdriver.Chrome()
    
        def end():
            driver.quit()
        request.addfinalizer(end)
        return driver

    # 自己用到项目的 封装到contest.py:

    import time
    
    import pytest
    from selenium import webdriver
    from test_data.locator_eles import Mylocator
    
    
    @pytest.fixture()
    def login():
        driver = webdriver.Chrome()
        driver.get("http://xxx.html")
        driver.implicitly_wait(10)
        driver.maximize_window()
        loc = Mylocator
        uname = loc.username_loc
        username = driver.find_element_by_css_selector(uname)
        username.send_keys("username")
    
        pwd = loc.password_loc
        password = driver.find_element_by_css_selector(pwd)
        password.send_keys("passwd")
    
        subbutton = loc.login_button_loc
        login_button = driver.find_element_by_css_selector(subbutton)
        login_button.click()
        yield driver
        driver.quit()
        return driver
  • 相关阅读:
    [转]多线程更新Processbar
    不能因技术后天的死 而迷茫了今天的“学” 生
    NSIS 安装包制作相关
    [转]yslow 评分标准
    c# winform 打印 窗体 及 窗体控件内容 的 初级尝试
    严重认知自身成长 与诸博友共勉
    [转]NSIS 的 Modern UI 教程
    爱的幸福
    遍历WinForm窗体 根据语言类型设置其控件Text显示
    多借鉴 多思考
  • 原文地址:https://www.cnblogs.com/wzhqzm/p/14234650.html
Copyright © 2011-2022 走看看