pytest

(十二)使用pytest管理用例:fixture和conftest使用

烂漫一生 提交于 2019-12-06 06:54:59
pytest下载: pip install pytest 查看pytest版本 pip list 回顾pytest基本用法: pytest 标记:@pytest.mark.名称,可对类或者方法进行标记 指定运行:pytest -m 名称 运行所有用例pytest(以test_开头) 需要import pytest pytest兼容unittest 以pytest方式运行,需要改该工程设置默认的运行器:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test conftest案例与用法: 案例:在pytest写法中如果test_1.py文件和test_*.py文件中都需要用到登录 用法: 1.在cases目录下新建一个conftest文件,前提该目录下有init.py文件 2.conftest.py配置脚本名称是固定的,不能改名称 3.不需要import导入 conftest.py,pytest用例会自动查找 4.通常与fixture一起使用 5.用例文件名以test开头 类名以test开头 方法名以test开头 conftest.py文件代码+讲解样式: import pytest from taobaoPO.pages.indexPage import

'function' object has no attribute 'assert_called_once_with'

ⅰ亾dé卋堺 提交于 2019-12-06 06:07:33
问题 I'm trying to run the following test using pytest and pytest_mock def rm(filename): helper(filename, 5) def helper(filename): pass def test_unix_fs(mocker): mocker.patch('module.helper') rm('file') helper.assert_called_once_with('file', 5) But I get exception AttributeError: 'function' object has no attribute 'assert_called_once_with' What am I doing wrong? 回答1: You can not perform a .assert_called_once_with function on a vanilla function: you first need to wrap it with the mock.create

Mocking a imported function with pytest [duplicate]

此生再无相见时 提交于 2019-12-06 05:14:56
This question already has answers here : Python Mocking a function from an imported module (2 answers) Closed last year . I would like to test a email sending method I wrote. In file, format_email.py I import send_email. from cars.lib.email import send_email class CarEmails(object): def __init__(self, email_client, config): self.email_client = email_client self.config = config def send_cars_email(self, recipients, input_payload): After formatting the email content in send_cars_email() I send the email using the method I imported earlier. response_code = send_email(data, self.email_client) in

Pytest - Fixture introspect on function level

浪子不回头ぞ 提交于 2019-12-06 03:22:01
I've got a fixture that requires a variable from the test function. Using introspection and declaring the variable in the function namespace/context should work if introspection on function level works, as it does for module level, but each time I run the code I end up with None instead of the string "Fancy Table". In the fixture I set the scope to 'function' and then introspect via getattr and request.function: #conftest.py @pytest.fixture(scope='function') def table(request): from data_setup import create_table table_name = getattr(request.function, "table_name", None) create_table(request,

pytest

倾然丶 夕夏残阳落幕 提交于 2019-12-06 02:17:47
快速入门 pytest是Python的单元测试框架,同自带的unittest框架类似,但pytest框架使用起来更简洁,效率更高。 pytest特点 入门简单易上手,文档支持较好。 支持单元测试和功能测试。 支持参数化。 可以跳过指定用例,或对某些预期失败的case标记成失败。 支持重复执行失败的case。 支持运行由unittest编写的测试用例。 有很多第三方插件,并且可自定义扩展。 方便和支持集成工具进行集成。 安装 pip install pytest 测试 C:\Users\Anthony>pytest --version This is pytest version 5.2.2, imported from c:\python36\lib\site-packages\pytest.py 在测试之前要做的准备 演示脚本处于这样一个的目录中: M:\py_tests\ # 我的是M盘的 py_tests 目录,所有操作都在 py_tests 目录内完成 ├─scripts │ ├─test_case_dir1 │ │ ├─test_case_02.py # 用例脚本文件 │ │ └─__init__.py │ ├─test_allure_case.py # 脚本文件 │ ├─test_case_01.py # 脚本文件 │ └─__init__.py ├─report │

What does Flaky: Hypothesis test produces unreliable results mean?

空扰寡人 提交于 2019-12-06 02:12:39
I am using the hypothesis python package for testing. I am getting the following error: Flaky: Hypothesis test_visiting produces unreliable results: Falsified on the first call but did not on a subsequent one As far as I can tell, the test is working correctly. How do I get around this? It means more or less what it says: You have a test which failed the first time but succeeded the second time when rerun with the same example. This could be a Hypothesis bug, but it usually isn't. The most common cause of this is that you have a test which depends on some external state - e.g. if you're using

How to get test name and test result during run time in pytest

非 Y 不嫁゛ 提交于 2019-12-06 01:06:47
问题 I want to get the test name and test result during runtime. I have setup and tearDown methods in my script. In setup , I need to get the test name, and in tearDown I need to get the test result and test execution time. Is there a way I can do this? 回答1: You can, using a hook. I have these files in my test directory: ./rest/ ├── conftest.py ├── __init__.py └── test_rest_author.py In test_rest_author.py I have three functions, startup , teardown and test_tc15 , but I only want to show the

pipenv使用学习

半腔热情 提交于 2019-12-06 00:07:46
参考https://realpython.com/pipenv-guide/#package-distribution Pipenv: A Guide to the New Python Packaging Tool Pipenv是Python的一个打包工具,它使用pip、virtualenv和旧的requirements.txt解决了与典型工作流相关的一些常见问题。 除了解决一些常见问题之外,它还将开发过程合并并简化为单个命令行工具。 本指南将详细讨论Pipenv解决了哪些问题,以及如何管理你与Pipenv的Python依赖关系。此外,还将介绍Pipenv如何适应以前的包分发方法。 Problems that Pipenv Solves 为了理解Pipenv的好处,了解当前Python中的打包和依赖项管理方法是很重要的。 让我们从处理第三方包的典型情况开始。然后,我们将构建部署完整Python应用程序的方法。 Dependency Management with requirements.txt 假设您正在处理一个使用类似于flask的第三方包的Python项目。您需要指定该需求,以便其他开发人员和自动化系统可以运行您的应用程序。 所以你决定在requirements.txt文件中包含flask依赖,requirements.txt如下: flask 很好,一切都在本地运行良好

pytest running with another version of python

谁说胖子不能爱 提交于 2019-12-06 00:05:04
问题 I've installed pyenv and have different versions of python installed with it: $ pyenv versions system 2.7.1 3.2.5 3.5.0 3.5.1 * 3.5.2 I use the following command to switch to python 3.5.2 : pyenv shell 3.5.2 And when I check the python version this is what I get: $ python --version Python 3.5.2 But when I run pytest , it still runs under python 2.7.6 : pytest -v ==================================================================== test session starts ===========================================

falcon, AttributeError: 'API' object has no attribute 'create'

人走茶凉 提交于 2019-12-05 23:32:53
I'm trying test my falcon routes, but tests always failed, and looks like I make all things right. my app.py import falcon from resources.static import StaticResource api = falcon.API() api.add_route('/', StaticResource()) and my test directory tests/static.py from falcon import testing import pytest from app import api @pytest.fixture(scope='module') def client(): # Assume the hypothetical `myapp` package has a # function called `create()` to initialize and # return a `falcon.API` instance. return testing.TestClient(api.create()) def test_get_message(client): result = client.simulate_get('/')