pytest

Can pytest be made to fail if nothing is asserted?

不问归期 提交于 2019-12-01 06:37:55
Today I had a failing test that happily succeeded, because I forgot a rather important line at the end: assert actual == expected I would like to have the machine catch this mistake in the future. Is there a way to make pytest detect if a test function does not assert anything, and consider this a test failure? Of course, this needs to be a "global" configuration setting; annotating each test function with @fail_if_nothing_is_asserted would defeat the purpose. This is one of the reasons why it really helps to write a failing test before writing the code to make the test pass. It's that one

unittest和pytest对比

[亡魂溺海] 提交于 2019-12-01 05:36:00
一、用例编写规则 1.unittest提供了test cases、test suites、test fixtures、test runner相关的类,让测试更加明确、方便、可控。使用unittest编写用例,必须遵守以下规则:   (1)测试文件必须先import unittest   (2)测试类必须继承unittest.TestCase   (3)测试方法必须以“test_”开头   (4)测试类必须要有unittest.main()方法  2.pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效。使用pytest编写用例,必须遵守以下规则: (1)测试文件名必须以“test_”开头或者"_test"结尾(如:test_ab.py)   (2)测试方法必须以“test_”开头。   (3)测试类命名以"Test"开头。  总结: pytest可以执行unittest风格的测试用例,无须修改unittest用例的任何代码,有较好的兼容性。 pytest插件丰富,比如flask插件,可用于用例出错重跑;还有xdist插件,可用于设备并行执行。      二、用例前置和后置 1.unittest提供了setUp/tearDown,每个用例运行前、结束后运行一次。setUpClass和tearDownClass,用例执行前

pytest官网文档の第一章:安装和快速开始

依然范特西╮ 提交于 2019-12-01 05:33:33
1.1 安装 安装命令 pip install pytest 查看版本 pytest --version 1.2 创建第一个测试函数 测试代码如下: # -*- coding: utf-8 -*- # 开发人员 :RayChou # 开发时间 :2019-10-12 10:27 # 文件名称 :test_simple.PY # 开发工具 :PyCharm def func(x): return x + 1 def test_answer(): assert func(3) == 5 执行结果如下: (venv) D:\workspace\demo1\test_module>pytest =========================================================================================================================== test session starts =========================================================================================================================== platform win32 -- Python 3.6.5,

Can pytest be made to fail if nothing is asserted?

£可爱£侵袭症+ 提交于 2019-12-01 04:18:50
问题 Today I had a failing test that happily succeeded, because I forgot a rather important line at the end: assert actual == expected I would like to have the machine catch this mistake in the future. Is there a way to make pytest detect if a test function does not assert anything, and consider this a test failure? Of course, this needs to be a "global" configuration setting; annotating each test function with @fail_if_nothing_is_asserted would defeat the purpose. 回答1: This is one of the reasons

Mocking a module import in pytest

a 夏天 提交于 2019-12-01 02:47:04
问题 I am writing a pytest plugin that should test software that's designed to work inside a set of specific environments. The software I'm writing is run inside a bigger framework, which makes certain Python modules available only when running my Python software inside the framework. In order to test my software, I'm required to "mock" or fake an entire module (actually, quite a few). I'll need to implement its functionality in some kind of similar-looking way, but my question is how should I

py.test : Can multiple markers be applied at the test function level?

梦想的初衷 提交于 2019-12-01 00:32:10
问题 I have seen from the pytest docs that we can apply multiple markers at once on the Class or module level. I didn't find documentation for doing it at the test function level. Has anybody done this before with success? I would like to ideally do this as a list of markers as being done in the above doc for Classes, for example (quoting from the docs): class TestClass: pytestmark = [pytest.mark.webtest, pytest.mark.slowtest] So, the pytest documentation talks about using pytestmark to specify

Chrome 59 support for basic auth credentials in URLs alternative for usage with Chromedriver?

不羁岁月 提交于 2019-11-30 23:22:33
With Chrome 59 the support for putting basic auth credentials in URLs - like https://foo:bar@www.foo.com has ended - this was warned a while ago within https://www.chromestatus.com/feature/5669008342777856 . Has anyone had to work around this with Selenium and Chromedriver yet? Specifically within Python? In our situation (automated testing using WebDriver via C# with NTLM auth) we found that once you hit the page with the credentials although you can't load the sub-resources on the page you are still authorized for that browser session. So we go to a page that we don't want to test (in our

How do I Pytest a project using PEP 420 namespace packages?

别等时光非礼了梦想. 提交于 2019-11-30 21:10:50
I am trying to use Pytest to test a largish (~100k LOC, 1k files) project and there are several other similar projects for which I'd like to do the same, eventually. This is not a standard Python package ; it's part of a heavily customized system that I have little power to change, at least in the near term. Test modules are integrated with the code, rather than being in a separate directory, and that is important to us. The configuration is fairly similar to this question , and my answer there that might provide helpful background, too. The issue I'm having is that the projects use PEP 420

pytest fixtures in a separate directory

做~自己de王妃 提交于 2019-11-30 20:57:24
I'm looking to create a pytest structure where I can separate the fixtures from the tests completely. The reason for this separation is that I want to include the fixtures directory as an external item in subversion and share it between multiple projects. tree of desired structure project | conftest.py | +---fixtures | __init__.py | conftest.py | fixture_cifs.py | fixture_ftp.py | fixture_service.py | \---tests | test_sometest1.py | test_sometest2.py | \---configurations sometest1.conf sometest2.conf I want to implement the functionality for each fixture in a separate file in order to avoid a

Pytest参数传递

故事扮演 提交于 2019-11-30 19:55:17
import pytest@pytest.fixture()def login_r(open_browser):#调用login时,发现需要先打开浏览器,所以改成先打开浏览器,在登陆 print('输入用户名密码登陆')@pytest.fixture()def open_browser(): print('打开浏览器')def test_cart(login_r): print('用例1,登陆后执行添加购物车功能操作')def test_search(): print('用例2,不登陆查询功能操作')def test_pay(login_r): print('用例3,登陆后执行支付功能操作') collecting ... collected 3 items 用例1,登陆后执行添加购物车功能操作 test_fixure.py::test_cart 打开浏览器 输入用户名密码登陆 用例2,不登陆查询功能操作 test_fixure.py::test_pay 用例3,登陆后执行支付功能操作 打开浏览器 输入用户名密码登陆 来源: https://www.cnblogs.com/QaStudy/p/11638483.html