pytest

How to use pytest capsys on tests that have mocking decorators?

自闭症网瘾萝莉.ら 提交于 2020-02-24 14:40:13
问题 I have being trying to find a way to use mocking decorators and pytest capsys at the same time but I wasn't able to find the right way to do it. import pytest import requests_mock @requests_mock.mock() def test_with_mock(m): pass def test_with_capsys(capsys): pass # how to write a test that works with both? 回答1: As stated in the request-mock's docs: pytest has its own method of registering and loading custom fixtures. requests-mock provides an external fixture registered with pytest such that

Test for import of optional dependencies in __init__.py with pytest: Python 3.5 /3.6 differs in behaviour

旧时模样 提交于 2020-02-24 03:54:10
问题 I have a package for python 3.5 and 3.6 that has optional dependencies for which I want tests (pytest) that run on either version. I made a reduced example below consisting of two files, a simple __init__.py where the optional package "requests" (just an example) is imported and a flag is set to indicate the availability of requests. mypackage/ ├── mypackage │ └── __init__.py └── test_init.py The __init__.py file content: #!/usr/bin/env python # -*- coding: utf-8 -*- requests_available = True

Using a fake mongoDB for pytest testing

不羁岁月 提交于 2020-02-21 12:53:10
问题 I have code that connects to a MongoDB Client and I'm trying to test it. For testing, I don't want to connect to the actual client, so I'm trying to figure out make a fake one for testing purposes. The basic flow of the code is I have a function somewhere the creates a pymongo client, then queries that and makes a dict that is used elsewhere. I want to write some tests using pytest that will test different functions and classes that will call get_stuff . My problem is that get_stuff calls

Using a fake mongoDB for pytest testing

孤人 提交于 2020-02-21 12:51:49
问题 I have code that connects to a MongoDB Client and I'm trying to test it. For testing, I don't want to connect to the actual client, so I'm trying to figure out make a fake one for testing purposes. The basic flow of the code is I have a function somewhere the creates a pymongo client, then queries that and makes a dict that is used elsewhere. I want to write some tests using pytest that will test different functions and classes that will call get_stuff . My problem is that get_stuff calls

Using a fake mongoDB for pytest testing

非 Y 不嫁゛ 提交于 2020-02-21 12:51:33
问题 I have code that connects to a MongoDB Client and I'm trying to test it. For testing, I don't want to connect to the actual client, so I'm trying to figure out make a fake one for testing purposes. The basic flow of the code is I have a function somewhere the creates a pymongo client, then queries that and makes a dict that is used elsewhere. I want to write some tests using pytest that will test different functions and classes that will call get_stuff . My problem is that get_stuff calls

Preventing truncation of long strings in pytest

吃可爱长大的小学妹 提交于 2020-02-21 10:22:10
问题 I have written a test harness for system tests of our code using pytest. These tests are used in our continuous integration system so I am using the junit xml output option. The truncation of long strings by pytest is causing me problems. I know I can prevent it using the -vv option but then that gives verbose output for the results of each test which is difficult to read. Essentially I want a different way to prevent the truncation of the long string at least in the junit xml file. If it

Preventing truncation of long strings in pytest

℡╲_俬逩灬. 提交于 2020-02-21 10:21:26
问题 I have written a test harness for system tests of our code using pytest. These tests are used in our continuous integration system so I am using the junit xml output option. The truncation of long strings by pytest is causing me problems. I know I can prevent it using the -vv option but then that gives verbose output for the results of each test which is difficult to read. Essentially I want a different way to prevent the truncation of the long string at least in the junit xml file. If it

How can I get pytest to ignore Test* classes that don't subclass unittest?

大城市里の小女人 提交于 2020-02-20 09:12:24
问题 I'm trying to get tests for testfixtures passing with pytest, but it keeps trying to collect things that aren't tests: ======================================================= pytest-warning summary ======================================================== WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_comparison.py cannot collect test class 'TestClassA' because it has a __init__ constructor WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_components.py cannot

pytest assert message customization with variable introspection

a 夏天 提交于 2020-02-20 07:35:29
问题 In the pytest documentation it says that you can customize the output message when an assert fails. I want to customize the assert message when testing a REST API method it returns an invalid status code: def test_api_call(self, client): response = client.get(reverse('api:my_api_call')) assert response.status_code == 200 So I tried to put a piece of code like this in conftest.py def pytest_assertrepr_compare(op, left, right): if isinstance(left, rest_framework.response.Response): return left

面试总结2-接口自动化

半腔热情 提交于 2020-02-19 11:05:11
前面总结了一篇关于接口测试的常规面试题,现在接口自动化测试用的比较多,也是被很多公司看好。那么想做接口自动化测试需要具备哪些能力呢? 也就是面试的过程中,面试官会考哪些问题,知道你是不是真的做过接口自动化测试?总的来说问的比较多的就是以下几个问题: 1.json和字典的区别? -对基础数据类型的考察 2.测试的数据你放在哪? -数据与脚本分离 3.参数化 - 数据驱动模式 4.下个接口请求参数依赖上个接口的返回数据 - 参数关联 5.依赖于登录的接口如何处理 -token和session的管理 6.依赖第三方的接口如何处理 -mock模拟数据返回 7.不可逆的操作,如何处理,比如删除一个订单这种接口如何测试 -造数据 8.接口产生的垃圾数据如何清理 - 数据清理 9.一个订单的几种状态如何全部测到,如:未处理,处理中,处理失败,处理成功 - 造数据,改数据库订单状态 10.python如何连接数据库操作? 11.其它的就是运行出报告、代码管理(git)、运行策略和持续集成jenkins相关了 1.json和字典dict的区别? 现在自动化培训烂大街,是个人都能说的上几个框架,面试如果问框架相关问题,求职者只需一瓶82年的雪碧,会吹的让你怀疑人生! 所以面试官为了更清楚的知道你是停留在表面上的花拳绣腿还是有扎实的基础,就不会问框架这种东西了。基本上问几个数据类型的基础就知道有没货了。