pytest

pytest 学习笔记一 入门篇

匆匆过客 提交于 2019-12-04 18:31:57
前言 之前做自动化测试的时候,用的测试框架为Python自带的unittest框架,随着工作的深入,发现了另外一个框架就是pytest (官方地址文档 http://www.pytest.org/en/latest/ ),这个框架支持的插件比较多,相对unittest来说,使用起来更加的强大,更加的方便,从今天开始就一步步学习pytest 简介 The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. 这是官方文档开头的一句话,大概意思就是说:pytest是一个可以让测试用例的编写变得简单,来支持测试复杂的程序应用和库 其特点: 上手简单,插件丰富,失败重试,报告美观。。。。。。。 环境搭建 安装 pip install pytest 简单入门 def inc(x): return x + 1 def test_answer(): assert inc(3) == 5  这是官方文档的一个例子 执行结果如下 来源: https://www.cnblogs.com/codeBang/p/11872545.html

pytest -- 测试的参数化

随声附和 提交于 2019-12-04 18:21:38
目录 1. @pytest.mark.parametrize 标记 1.1. empty_parameter_set_mark 选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_generate_tests 钩子方法 往期索引: https://www.cnblogs.com/luizyao/p/11771740.html 在实际工作中,测试用例可能需要支持多种场景,我们可以把和场景强相关的部分抽象成参数,通过对参数的赋值来驱动用例的执行; 参数化的行为表现在不同的层级上: fixture 的参数化:参考 4、fixtures:明确的、模块化的和可扩展的 -- fixture 的参数化 ; 测试用例的参数化:使用 @pytest.mark.parametrize 可以在测试用例、测试类甚至测试模块中标记多个参数或 fixture 的组合; 另外,我们也可以通过 pytest_generate_tests 这个钩子方法自定义参数化的方案; 1. @pytest.mark.parametrize 标记 @pytest.mark.parametrize 的根本作用是在 收集 测试用例的过程中,通过对 指定参数 的赋值来新增被标记对象的 调用(执行) ; 首先,我们来看一下它在源码中的定义: # _pytest/python.py def parametrize

maintaining order of test execution when parametrizing tests in test class

核能气质少年 提交于 2019-12-04 18:13:20
I am trying to parametrize my tests like below @pytest.mark.parametrize("a,b", test_data) class TestClass(): def test_A(self,a,b): # Some Code .. pass def test_B(self,a,b): # Some Code .. pass def test_C(self,a,b): # Some Code .. pass I want my test to be executed in Sequential order like test steps, e.g test_A test_B test_C test_A test_B test_C .... The order in which they are getting executed is test_A test_A ... test_B test_B ... test_C test_C The other option I have tried is by putting my tests in for loop like below for data in test_data: a,b = data def test_A(a,b): # Some Code .. pass

pytest-timeout - fail test instead killing whole test run

自闭症网瘾萝莉.ら 提交于 2019-12-04 17:54:48
I know in pytest-timeout I can specify timeout for each testcase but single failure terminates whole test run instead failing the slacking off testcase. Am I forced to make my own solution of this or there are ready-to-use tools which provide that? I looked into this issue a long time ago and also came to the conclusion that a self-made solution would be better. My plugin was killing the whole pytest process, but it can be adjusted to fail only a single (current) test easily. Here is the adjusted draft: import pytest import signal class Termination(SystemExit): pass class TimeoutExit

How do you configure PyCharm to run py.test with command-line options like -s?

情到浓时终转凉″ 提交于 2019-12-04 17:15:14
问题 I figured out how to run py.test files from PyCharm: Run/Debug configurations in the Python tests category, click the "+" button to add a new configuration choose py.test enter the full path for the Target script and the working directory but I can't seem to add the -s option to allow my test script to run with standard output not captured and hidden. (I tried -s under interpreter options but it appears not to do anything.) How can I enable -s ? 回答1: D'oh, I figured it out after all: You add

pytest run tests parallel

北慕城南 提交于 2019-12-04 15:32:18
问题 I want to run all my pytest tests in parallel instead of sequentially. my current setup looks like: class Test1(OtherClass): @pytest.mark.parametrize("activity_name", ["activity1", "activity2"]) @pytest.mark.flaky(reruns=1) def test_1(self, activity_name, generate_test_id): """ """ test_id = generate_random_test_id() test_name = sys._getframe().f_code.co_name result_triggers = self.proxy(test_name, generate_test_id, test_id, activity_name) expected_items = ["response"] validate_response(

Why does the simplest requests_mock example fail with pytest?

左心房为你撑大大i 提交于 2019-12-04 13:03:31
I have a peculiar problem with requests_mock . I want to use it with pytest to test my API wrapper library. I've tried to use the first example in the requests_mock docs , except I put it in a test_mock() -function and added an assert -statement for pytest to discover it. The following code fails: # tests/test_mock.py import requests import requests_mock with requests_mock.Mocker() as m: def test_mock(): m.get('http://test.com', text='resp') assert requests.get('http://test.com').text == 'resp' However, when running the following example in ipython, it works as expected. This is the exact code

Change pytest testsuite name in xml report

廉价感情. 提交于 2019-12-04 12:53:21
Because we started same tests with different interface, then take all reports and send them to jenkins. It's difficult to recognise with what interface we have errors. Before we use nose, it has parameter "--xunit-testsuite-name", has pytest analog? I want to change name of test suite in reports testsuite errors="0" failures="0" name=" pytest " skips="0" tests="12" time="103.702" to testsuite errors="0" failures="0" name=" inteface1 " skips="0" tests="12" time="103.702" You can add a junit_suite_name attribute to your Pytest config file since pytest 3.1: [pytest] junit_suite_name = my_suite

In which py.test callout can I find both 'item' and 'report' data?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 12:17:40
问题 pytest_runtest_makereport() gets two arguments, item and call. From item, I can find the funcarg I created for this test, and from call, I can find the exception info (if any): def pytest_runtest_makereport (item, call): my_funcarg = item.funcargs['name'] my_funcarg.excinfo = call.excinfo Unfortunately, excinfo is populated for both failures and for skips. To distinguish, I need to look at the report argument to pytest_report_teststatus(): def pytest_report_teststatus (report): if report.when

How to keep track of instances of python objects in a reliable way?

自闭症网瘾萝莉.ら 提交于 2019-12-04 12:15:55
问题 I would like to be able to keep track of instances of geometric Point objects in order to know what names are already "taken" when automatically naming a new one. For instance, if Points named "A", "B" and "C" have been created, then the next automatically named Point is named "D". If Point named "D" gets deleted, or its reference gets lost, then name "D" becomes available again. The main attributes of my Point objects are defined as properties and are the quite standard x , y and name .