为什么选择Python?Python是当前最流行的编程语言之一。它为Web后端,数据科学笔记本,sysadmin脚本等提供支持。它的语法简洁,易读且优雅–非常适合初学者和专家。您可以想象的一切都只是一个导入。自然地,Python还是测试自动化的最好的语言。它的简洁性使测试人员可以将更多的精力放在测试上,而不必在代码上。未完成大量编程工作的测试人员往往比其他语言(如Java或C#)学习Python的速度更快。Python非常适合启动测试! 什么是pytest?任何功能测试自动化项目的核心都是“核心”测试框架。该框架处理测试用例结构,测试执行以及通过/失败结果报告。这是可以添加额外的程序包和代码(例如Selenium WebDriver)的基础。 pytest是Python最好的测试框架之一。它简单,可扩展且具有Python风格。测试用例是作为函数而不是类编写的。测试断言失败将与实际值一起报告。插件可以添加代码覆盖率,漂亮的报告和并行执行。pytest也可以与Django和Flask等其他框架集成。根据2018年Python开发人员调查,它也是最受欢迎的Python测试框架。 入门让我们创建我们的Python测试项目!如果您尚未这样做,请下载并在您的计算机上安装Python 3。然后,为项目创建一个新目录: mkdir python-webui-testing cd python-webui-testing 每当我创建一个新的Python项目时,都会为其依赖项创建一个虚拟环境。这样,同一台计算机上的项目就不会有相互冲突的软件包版本。我使用pipenv 是因为它简化了工作流程。要全局安装pipenv,请运行: pip install pipenv 然后,为新项目安装pytest: $ pipenv install pytest --dev Pipenv将向您的项目添加两个新文件: 第一次测试按照惯例,大多数项目会将所有测试放在一个 目录下。让我们遵循以下约定: mkdir tests cd tests 创建一个 为我们的第一个测试命名的Python模块,并添加以下代码: def test_addition(): assert 1 + 1 == 2 使用pytest编写的测试通常不需要太多代码。这两行是功能齐全的测试用例!测试用例是作为函数而不是类编写的。像这样的基本测试不需要导入。使用Python的本机 运行测试让我们运行我们的新测试。将目录更改回项目根目录,并调用pytest模块: $ cd .. $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 1 item tests/test_math.py . [100%] =========================== 1 passed in 0.02 seconds =========================== 我们的第一个测试通过了! pytest是如何发现我们的测试的?按名称:pytest将搜索名为 的模块中命名的 测试函数 。有趣的是,pytest不需要任何测试目录中的文件。 测试失败如果测试失败,会发生什么?让我们添加另一个带有错误的测试来找出: def test_subtraction(): diff = 1 - 1 assert diff == 0 让我们重新运行这些测试: $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 2 items tests/test_math.py .. [100%] =========================== 2 passed in 0.02 seconds =========================== 我们回到了正轨。 参数化测试如果我们要使用多个输入组合来运行相同的测试过程,该怎么办?pytest有一个装饰器!让我们编写一个新的参数化输入乘法测试: import pytest
"a,b,expected", [(0, 5, 0), (1, 5, 5), (2, 5, 10), (-3, 5, -15), (-4, -5, 20)]) def test_multiplication(a, b, expected): assert a * b == expected 这次, $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 7 items tests/test_math.py ....... [100%] =========================== 7 passed in 0.03 seconds =========================== 参数是进行数据驱动测试的好方法。 验证异常pytest将未处理的异常视为测试失败。实际上,该 def test_divide_by_zero(): with pytest.raises(ZeroDivisionError): 1 / 0 重新运行测试以确保一切正常: $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 8 items tests/test_math.py ........ [100%] =========================== 8 passed in 0.04 seconds =========================== ![]() ![]() |
|
来自: 软件测试test > 《python自动化》