pytest

【python】如何使用全功能的Python测试框架(小白白必看系列)

拥有回忆 提交于 2019-11-26 17:20:50
如何使用pytest 大纲 安装和简单使用 配置文件 笔记来自: http://stu.ityxb.com/index.html#/ansandqus 断言 一. 第一步 —— 安装和简单使用 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: • 1、简单灵活,容易上手,文档丰富; • 2、支持参数化,可以细粒度地控制要测试的测试用例; • 3、能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests); • 4、pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等; • 5、测试用例的skip和xfail处理; • 6、可以很好的和CI工具结合,例如jenkins 安装 pip3 install pytest 简单使用 新建一个test_sample.py文件,输入以下代码: def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 在test_sample

Pytest单元测试框架-环境安装

我只是一个虾纸丫 提交于 2019-11-26 17:06:19
unittest是python自带的单元测试框架,它封装好了一些校验返回的结果方法和一些用例执行前的初始化操作,使得单元测试易于开展,因为它的易用性,很多同学也拿它来做功能测试和接口测试,只需简单开发一些功能(报告,初始化webdriver,或者http请求方法)便可实现。 但自动化测试中我们常常需要根据不同需求挑选部分测试用例运行,并且我们希望用例克服环境不稳定的局限,即运行失败后自动重新运行一次,如果成功就认为是环境问题导致第一次失败,还有我们经常希望测试用例可以并发执行等等,这些unittest都做不到或者需要大量二次开发才能做到,那么有没有更加强大的框架可以替代unittests呢? pytest是python里的一个强大框架,它可以用来做单元测试,你也可以用来做功能,接口自动化测试。而且它比unittest支持的功能更多更全面。但是pytest在Getstarted里给出的实例却很简单,很多同学错以为它只是跟unittest一样是个单元测试框架罢了,如果你查询中文互联网,你也只能找到寥寥数篇大致一样的用法,可以说pytest的精髓使用,没有被大家挖掘出来,如此强大的框架不应该被埋没,今天我就带领大家深入pytest使用,共同领略pytest的强大。 1.安装pytest单元测试框架 2.检查Pytest安装版本 使用的命令是:pip show pytest 也可以使用

Selenium - visibility_of_element_located: __init__() takes exactly 2 arguments (3 given)

爷,独闯天下 提交于 2019-11-26 16:58:21
问题 I am getting this error in my test code that uses Selenium Python Bindings: > twitter_campaigns = wait.until(EC.visibility_of_element_located(By.CSS_SELECTOR, TWITTER_CAMPAIGNS)) E TypeError: __init__() takes exactly 2 arguments (3 given) And this is what Im executing: class TestTwitter(TestLogin, TestBuying): def setup(self, timeout=10): self.driver = webdriver.Firefox() self.driver.get(BASEURL) self.driver.implicitly_wait(timeout) def test_campaigns_loaded(self, timeout=10): self.signin

pytest前置后置条件

江枫思渺然 提交于 2019-11-26 16:10:06
已知被测对象demo.py #!/usr/bin/python # -*- coding: utf-8 -*- def add(a, b): return a+b def minus(a, b): return a-b1.测试函数案例 test_demo_module.py 文件必须要 test_*.py 和 *_test.py 来命名 测试case命名必须要 test 开头,最好是 test_ 判断使用 assert setup_module, 所有case 执行的前置条件,只运行 一次 teardown_module, 所有case 执行的后置条件,只运行 一次 setup_function, 每个测试用例 的前置条件 teardown_function, 每个测试用例 的后置条件 2.测试类案例 test_demo_class.py 文件必须要 test_*.py 和 *_test.py 来命名 测试类命名必须要 Test 开头 测试case命名必须要 test 开头,最好是 test_ 判断使用 assert setup_class, 所有case 执行的前置条件,只运行 一次 teardown_class, 所有case 执行的后置条件,只运行 一次 setup, 每个测试用例 的前置条件 teardown, 每个测试用例 的后置条件 3. pytest执行命令:

How to properly assert that an exception gets raised in pytest?

谁都会走 提交于 2019-11-26 15:52:58
Code: # coding=utf-8 import pytest def whatever(): return 9/0 def test_whatever(): try: whatever() except ZeroDivisionError as exc: pytest.fail(exc, pytrace=True) Output: ================================ test session starts ================================= platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2 plugins: django, cov collected 1 items pytest_test.py F ====================================== FAILURES ====================================== ___________________________________ test_whatever ____________________________________ def test_whatever(): try: whatever() except

pytest+allure(allure-pytest基于这个插件)生成漂亮的报告+显示

泪湿孤枕 提交于 2019-11-26 14:34:55
一:环境准备 1.python3.6 2.windows环境 3.pycharm 4.allure-pytest 5.allure2.8.0 6.java1.8 allure-pytest快速安装 在cmd中输入 pip install allure-pytest,回车 二:报告生成 第1步:下载allure.zip,下载地址:allure-github: https://github.com/allure-framework/allure2 ,找到对应版本,并下载 第2步:解压allure.zip,将路径添加环境变量,path中,记得需要重启电脑 第3步:验证allure,在cmd中输入allure,然后回车,如果可以看到一下,说明配置完成 第4步:运行测试用例 pytest.main(["-m","login","-s","-q","--alluredir=./report"]) "-m": 标记用例 "login": 被标记需要执行用例 "-s":允许终端在测试运行时输出某些结果,例如你想输入print的内容,可以加上-s "-q"简化输出结果 "--alluredir": 生成allure指定语法 "./report":生成报告的路径 "--clean-alluredir" :因为这个插件库allure-pytest生成的报告文件,你第二次运行时候不会清理掉里面的东西

pytest文档19-pytest分布式执行(pytest-xdist)

假如想象 提交于 2019-11-26 13:04:01
转载地址: https://www.cnblogs.com/yoyoketang/p/9775646.html 前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟。如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试人力成本换取时间成本,这个时候多找个小伙伴把任务分成2部分,于是时间缩减一半。如果是十个人一起执行,1000个用例理论上只需100分钟就能完成,时间缩短到了1/10。大大节省的测试时间,为项目节省了时间成本。 同样道理,当我们测试用例非常多的时候,一条条执行,很显然会比较慢,那么如何让测试用例并行执行呢,这就是我们接下来要讲的pytest分布式执行插件pytest-xdist pytest-xdist cmd里面使用pip安装,目前版本号Version: 1.23.2 pip install pytest-xdist >pip show pytest-xdist Name: pytest-xdist Version: 1.23.2 Summary: pytest xdist plugin for distributed testing and loop-on-failing modes Home-page: https://github.com/pytest-dev/pytest-xdist Author:

pytest文档9-参数化parametrize

∥☆過路亽.° 提交于 2019-11-26 12:50:31
转载地址: https://www.cnblogs.com/yoyoketang/p/9455276.html 前言 pytest.mark.parametrize装饰器可以实现测试用例参数化。 parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of test_expectation.py # coding:utf-8 import pytest @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), ("6 * 9", 42), ]) def test_eval(test_input, expected): assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"]) 运行结果 ================================== FAILURES =================================== _____________________________ test_eval[6 * 9-42] _____________________________

Pass a parameter to a fixture function

拜拜、爱过 提交于 2019-11-26 11:50:41
问题 I am using py.test to test some DLL code wrapped in a python class MyTester. For validating purpose I need to log some test data during the tests and do more processing afterwards. As I have many test_... files I want to reuse the tester object creation (instance of MyTester) for most of my tests. As the tester object is the one which got the references to the DLL\'s variables and functions I need to pass a list of the DLL\'s variables to the tester object for each of the test files

Test case execution order in pytest

别说谁变了你拦得住时间么 提交于 2019-11-26 11:28:36
问题 I am using pytest. I have two files in a directory. In one of the files there is a long running test case that generates some output. In the other file there is a test case that reads that output. How can I ensure the proper execution order of the two test cases? Is there any alternative other than puting the test cases in the same file in the proper order? 回答1: In general you can configure the behavior of basically any part of pytest using its well-specified hooks. In your case, you want the