美文网首页自动化测试笔记
Selenium(二)——用pytest编写测试用例

Selenium(二)——用pytest编写测试用例

作者: 小楼_987f | 来源:发表于2019-05-20 14:05 被阅读15次

上一小节 Selenium(一)——Page Object Model (POM) 已经把新浪微博登陆页的页面对象编写完成,接下来继续编写测试用例。

1. 简介

  • 先安装两个python包:
    pip install pytest
    pip install pytest-html
    pytest 是一个测试框架,可以轻松构建简单且可扩展的测试。pytest-html
    是 pytest 的一个插件,它为测试结果生成一个HTML报告。

  • pytest 中的 fixture
    pytest 中的 fixture 可以类比于 unittest 中的 setUp&tearDown,但是又在此基础上有所改进,比如支持参数化等。

  • pytest-html 的报告格式
    为了遵守内容安全策略(CSP),默认情况下运用命令:pytest --html=report.html生成的测试报告会单独存储CSS和图像等多个资源。您也可以创建一个独立的报告,在共享结果时更方便。这可以通过以下命令完成:
    pytest --html=report.html --self-contained-html

2. 编写测试用例

运用 pytest 的参数化功能@pytest.mark.parametrize,为同一个测试方法传入不同的测试数据,可以将一个测试用例拓展成多个,避免了冗余代码。
参数化过程很简单,先准备好测试数据,在“新浪登陆页面测试”中测试数据是多个元组拼成的一个列表,每个元组中装载了各不相同的帐号、密码、检查信息:

testdata = [('', '', '请输入登录名'),   # 帐号、密码为空
            ('haha', '', '请输入密码'),     # 密码为空
            ('', 'hehe', '请输入登录名'),   # 帐号为空
            ('haha', 'hehe', '用户名或密码错误。查看帮助'),     # 帐号、密码错误
            ('haha@163.com', 'hehe', '请填写验证码'),       # 密码错误
]

然后,用 pytest 提供的@pytest.mark.parametrize装饰器,将数据绑定到测试方法上,就可以在方法中直接取用啦:

@pytest.mark.parametrize("username, password, message", testdata)
def test_login(self, username, password, message):

test_xl_login.py 内容如下:

  1 import pytest
  2 from selenium import webdriver
  3 from xl_login import LoginPage
  4 
  5 
  6 # 准备好测试数据
  7 testdata = [('', '', '请输入登录名'),   # 帐号、密码为空
  8             ('haha', '', '请输入密码'),     # 密码为空
  9             ('', 'hehe', '请输入登录名'),   # 帐号为空
 10             ('haha', 'hehe', '用户名或密码错误。查看帮助'),     # 帐号、密码错误
 11             ('haha@163.com', 'hehe', '请填写验证码'),       # 密码错误
 12 ]
 13 
 14 # 定义fixture,class内有效
 15 @pytest.fixture(scope="class")
 16 def login_ini(request):
 17     """
 18     初始化浏览器和登陆页面
 19     完成测试后,结束driver
 20     """
 21     # 预打开页面
 22     base_url = 'https://weibo.com/'
 23     # 页面title
 24     title = '微博-随时随地发现新鲜事'
 25     # 打开Chrome浏览器
 26     driver = webdriver.Chrome()
 27     # 登陆页面初始化
 28     request.cls.login = LoginPage(driver, base_url, title)
 29     # 卸载夹具,以下代码将在最后一个测试用例结束后执行
 30     yield login_ini
 31     driver.quit()
 32 
 33 # 使用fixture
 34 @pytest.mark.usefixtures("login_ini")
 35 class TestLogin():
 36     # 参数化
 37     @pytest.mark.parametrize("username, password, message", testdata)
 38     def test_login(self, username, password, message):
 39         """
 40         测试用例
 41         """
 42         login = self.login
 43         login.open()
 44         # 输入用户名
 45         login.type_username(username)
 46         # 输入密码
 47         login.type_password(password)
 48         # 点击登陆
 49         login.submit()
 50         # 获取提示框信息
 51         ms = login.get_message()
 52         # 判定检测
 53         assert ms == messagegok

现在,创建一个 conftest.py 文件,用于配置测试报告,以免报告中的中文字符无法选择正确的编码:

  1 from datetime import datetime
  2 from py.xml import html
  3 import pytest
  4 
  5 
  6 @pytest.mark.hookwrapper
  7 def pytest_runtest_makereport(item, call):
  8     outcome = yield
  9     report = outcome.get_result()
 10     # 对test一列重新编码,显示中文
 11     report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")

3. 运行测试用例

现在来试着运行一下测试用例:

>>> pytest ./test_xl_login.py --html=report.html --self-contained-html
============================================== test session starts ==============================================
platform linux -- Python 3.7.3, pytest-4.4.1, py-1.8.0, pluggy-0.10.0
rootdir: /home/user1/test/PageObject
plugins: metadata-1.8.0, html-1.20.0
collected 5 items                                                                                               

test_xl_login.py .....                                                                                    [100%]

--------------------- generated html file: /home/user1/test/PageObject/report.html ---------------------
=========================================== 5 passed in 27.18 seconds ===========================================
看看测试报告: 新浪微博登陆页面测试报告.png

相关文章

  • Selenium(二)——用pytest编写测试用例

    上一小节 Selenium(一)——Page Object Model (POM) 已经把新浪微博登陆页的页面对象...

  • pytest使用步骤&查看执行结果

    >>pytest使用步骤 1、导入pytest 2、编写测试用例:1)无需在测试类下编写测试用例可以直接编写测试函...

  • Pytest

    pytest pytest 可以用来做 系统测试 的自动化, 它的特点有 用 Python 编写测试用例,简便易用...

  • Pytest-selenium简单使用示例

    Pytest-selenium是Pytest的一个插件, 安装方法: 用例编写时只需要在测试函数中传入seleni...

  • 软件测试常见问题

    1、软件测试流程是什么? ①需求分析,需求评审②编写测试计划③编写测试用例,用例评审④执行测试,提交bug,回归测...

  • APP功能测试点总结(转载)

    1.功能性测试: ——根据产品需求文档编写测试用例。 ——软件设计文档编写用例。 注意:就是根据产品需求文档编写测...

  • APP测试点全面总结(上)

    1.功能性测试: ——根据产品需求文档编写测试用例。 ——软件设计文档编写用例。 注意:就是根据产品需求文档编写测...

  • APP测试点总结(全面)

    1.功能性测试: ——根据产品需求文档编写测试用例。 ——软件设计文档编写用例。 注意:就是根据产品需求文档编写测...

  • pytest常用:fixture、传参、标记、重运行

    一、pytest unittest和pytest区别: 共同点: 测试框架 都是通过代码来构建测试用例、收集用例、...

  • pytest执行报错AttributeError: 'NoneT

    问题现象在使用selenium+ pytest测试框架,执行自动化测试用例报错,命令行提示:“INTERNALER...

网友评论

    本文标题:Selenium(二)——用pytest编写测试用例

    本文链接:https://www.haomeiwen.com/subject/opdhzqtx.html