pytest

Pytest 使用简介

China☆狼群 提交于 2019-11-30 13:17:26
前言   最近在听极客时间的课程,里面的讲师极力推崇 pytest 框架,鄙视 unittest 框架,哈哈!然后查了些资料,发现了一条 python 鄙视链: pytest 鄙视 > unittest 鄙视 > robotframework 。   pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效,支持315种以上的插件,同时兼容 unittest 框架。这就使得我们在 unittest 框架迁移到 pytest 框架的时候不需要重写代码。接下来我们在文中来对分析下 pytest 有哪些简洁、高效的用法。 一、安装 首先使用 pip 安装 pytest pip3 install pytest 查看 pytest 是否安装成功 pip3 show pytest 二、简单使用 1.创建 test_sample.py 文件,代码如下: #!/usr/bin/env python # coding=utf-8 import pytest def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 if __name__ =="__main__": pytest.main() 执行结果: test_sample.py F [100%] =======================

Can params passed to pytest fixture be passed in as a variable?

两盒软妹~` 提交于 2019-11-30 12:45:02
I have two simple test setups and I'm trying to group them in one fixture and want the test function to pass in the 'params' to the fixture. Here's a contrived example, to explain my question. Say I have the following pytest fixture: @pytest.fixture(scope="module", params=['param1','param2']) def myFixture(request): if request.param == 'param1': p = 5 elif request.param == 'param2': p = 10 return p # would like to set request.param = ['param1'] for myFixture def test_madeup(myFixture): assert myFixture == 5 # would like to set request.param = ['param2'] for myFixture def test_madeup2(myFixture

py.test to test flask register, AssertionError: Popped wrong request context

与世无争的帅哥 提交于 2019-11-30 11:28:36
I'm using flask to do register and login: from flask.ext.security.views import register, login class Register(Resource): def post(self): return register() class Login(Resource): def post(self): return login() api.add_resource(Login, '/login') api.add_resource(Register, '/register') then I use py.test to test the class: class TestAPI: def test_survey(self, app): client = app.test_client() data = {'email': 'test@test', 'password': 'password'} rv = client.post('/2014-10-17/register', data=json.dumps(data)) ... when I ran the test, the error occurred as follow: AssertionError: Popped wrong request

Is there a way to change the location of pytest's .cache directory?

旧巷老猫 提交于 2019-11-30 11:12:30
I need to be able to change the location of pytest's .cache directory to the env variable, WORKSPACE. Due to server permissions out of my control, I am running into this error because my user does not have permission to write in the directory where the tests are being run from: py.error.EACCES: [Permission denied]: open('/path/to/restricted/directory/tests/.cache/v/cache/lastfailed', 'w') Is there a way to set the path of the .cache directory to the environment variable WORKSPACE? You can prevent the creation of .cache/ by disabling the "cacheprovider" plugin: py.test -p no:cacheprovider ...

Python project directory structure / pytest trouble

青春壹個敷衍的年華 提交于 2019-11-30 10:57:05
问题 This should be the easiest problem on earth, but even after extensive searching and tinkering, I'm still in deep trouble with finding a "correct" way to lay a directory structure and manage to run pytest etc correctly. Let's say my I have a program called apple. |- README.md |- apple | |-- __init__.py | |-- apple.py | - tests | |-- test_everything.py The apple.py contains some functions, for examples sake let's call one eat() . And the test_everything.py file contains some tests like assert

How to I display why some tests where skipped while using py.test?

☆樱花仙子☆ 提交于 2019-11-30 10:45:44
I am using skipIf() from unittest for skipping tests in certain conditions. @unittest.skipIf(condition), "this is why I skipped them!") How do I tell py.test to display skipping conditions? I know that for unittest I need to enable the verbose mode ( -v ) but the same parameter added to py.test increase the verbosity by still does not display the skip reasons. Todd Wilson When you run py.test, you can pass -rsx to report skipped tests. From py.test --help : -r chars show extra test summary info as specified by chars (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed. Also see this part of

How to use pytest to check that Error is NOT raised

社会主义新天地 提交于 2019-11-30 10:42:29
Let's assume we have smth like that : import py, pytest ERROR1 = ' --- Error : value < 5! ---' ERROR2 = ' --- Error : value > 10! ---' class MyError(Exception): def __init__(self, m): self.m = m def __str__(self): return self.m def foo(i): if i < 5: raise MyError(ERROR1) elif i > 10: raise MyError(ERROR2) return i # ---------------------- TESTS ------------------------- def test_foo1(): with pytest.raises(MyError) as e: foo(3) assert ERROR1 in str(e) def test_foo2(): with pytest.raises(MyError) as e: foo(11) assert ERROR2 in str(e) def test_foo3(): .... foo(7) .... Q: How can I make test_foo3(

Use docstrings to list tests in py.test

ぃ、小莉子 提交于 2019-11-30 08:11:43
Here is a simple test file: # test_single.py def test_addition(): "Two plus two is still four" assert 2 + 2 == 4 def test_addition2(): "One plus one is still two" assert 1 + 1 == 2 The default output in py.test is like $ py.test test_single.py -v [...] test_single.py::test_addition PASSED test_single.py::test_addition2 PASSED I would like to have Two plus two is still four PASSED One plus one is still two PASSED i.e. use the docstrings as descriptions for the tests. I tried to use a customization in a conftest.py file: import pytest @pytest.mark.tryfirst def pytest_runtest_makereport(item,

Using py.test with coverage doesn't include imports

风格不统一 提交于 2019-11-30 06:36:43
问题 For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didn't help. We're using py.test as a test runner. However, we are unable to add the imports and other "imported" stuff to the report. For example __init__.py is always reported as being uncovered: Name Stmts Miss Cover -------------------------------------------------- jedi/__init__ 5 5 0% [..] Clearly this file is being imported and should therefore be reported as tested. We start tests like

多线程并行与分布式执行

Deadly 提交于 2019-11-30 06:20:17
场景:测试用例1000条,一个用例执行1钟,一个测试人员执行需要1000分 钟。通常我们会用人力成本换取时间成本,加几个人一起执行,时间就会缩 短。如果10人一起执行只需要100分钟,这就是一种并行测试,分布式场景。 解决:pytest分布式执行插件:pytest-xdist,多个CPU或主机执行 前提:用例之间都是独立的,没有先后顺序,随机都能执行,可重复运行不 影响其他用例。 安装:Pip3 install pytest-xdist • 多个CPU并行执行用例,直接加-n 3是并行数量:pytest -n 3 • 在多个终端下一起执行 来源: https://www.cnblogs.com/QaStudy/p/11567166.html