pytest

Mocking with PyTest: Improving my XML write/parse unit test

牧云@^-^@ 提交于 2020-04-18 05:48:37
问题 So I am rather new with pytest and mock , but still have experience with junit and mocking with mockito for groovy (which comes with an handy when(...).thenAnswer/Return function) I wrote a simple class to parse and write xml files. This class sole purpose for existence is to be mocked in order to unit test the plugin I am currently working on. This personal project is also used as a learning tool to help me in my work duties (devOps python based) Obviously, I needed to test it too. Here is

pytest: setup a mock for every test function

对着背影说爱祢 提交于 2020-04-17 21:32:51
问题 Creating unit tests for a python project we're reaching this kind of 'template' from unittest import TestCase from unittest.mock import patch, Mock @patch('......dependency1') @patch('......dependency2') @patch('......dependencyn') class MyTest(TestCase): def test_1(self, mock1, mock2, mockn): # setup mock1 & mock2... # call the subject case 1 # assert response and/or interactions with mock1 and mock2... def test_2(self, mock1, mock2, mockn): # setup mock1 & mock2... # call the subject case 2

Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用

怎甘沉沦 提交于 2020-04-16 22:20:40
【推荐阅读】微服务还能火多久?>>> 如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 平常我们功能测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟,如果单个测试人员执行需要1000分钟才能跑完 当项目非常紧急时,会需要协调多个测试资源来把任务分成两部分,于是执行时间缩短一半,如果有10个小伙伴,那么执行时间就会变成十分之一,大大节省了测试时间 为了节省项目测试时间,10个测试同时并行测试,这就是一种分布式场景 同样道理,当我们自动化测试用例排常多的时候, 一条条按顺序执行会非常慢, pytest-xdist的出现就是为了让自动化测试用例可以分布式执行,从而节省自动化测试时间 pytest-xdist是属于进程级别的并发 分布式执行用例的设计原则(重中之重的重点) 用例之间是独立的,用例之间没有依赖关系,用例可以完全独立运行 【独立运行】 用例执行没有顺序,随机顺序都能正常执行 【随机执行】 每个用例都能重复运行,运行结果不会影响其他用例 【不影响其他用例】 插件安装 pip3 install pytest-xdist -i http: // pypi.douban.com/simple/ --trusted-host pypi.douban

How to measure coverage when using multirpocessing via pytest?

大憨熊 提交于 2020-04-16 05:12:47
问题 I run my unit tests via pytest. For coverage I use coverage.py. In one of my unit tests, I run a function via multirpocessing and the coverage does not reflect the functions running via multirpocessing , but the asserts work. That's the problem I am trying to solve. The test looks like so: import time import multiprocessing def test_a_while_loop(): # Start through multiprocessing in order to have a timeout. p = multiprocessing.Process( target=foo name="Foo", ) try: p.start() # my timeout time

How to measure coverage when using multirpocessing via pytest?

前提是你 提交于 2020-04-16 05:09:13
问题 I run my unit tests via pytest. For coverage I use coverage.py. In one of my unit tests, I run a function via multirpocessing and the coverage does not reflect the functions running via multirpocessing , but the asserts work. That's the problem I am trying to solve. The test looks like so: import time import multiprocessing def test_a_while_loop(): # Start through multiprocessing in order to have a timeout. p = multiprocessing.Process( target=foo name="Foo", ) try: p.start() # my timeout time

Add local variables of test functions of pytest to make a separate column in pytest-csv

谁说胖子不能爱 提交于 2020-04-15 06:50:01
问题 I am trying to add columns in the logs generated by pytest-csv and fill it using the local variables of my test function. My test function is something like this: def test_login(browser): search_page = SearchPage(browser) search_page.load() login_success = search_page.login() assert login_success=='Proceed' I want to add value of login_success variable in the column. There is a way to do it using properties and properties_as_columns but I am not able to figure out how to do that. Please help

Add local variables of test functions of pytest to make a separate column in pytest-csv

北战南征 提交于 2020-04-15 06:49:08
问题 I am trying to add columns in the logs generated by pytest-csv and fill it using the local variables of my test function. My test function is something like this: def test_login(browser): search_page = SearchPage(browser) search_page.load() login_success = search_page.login() assert login_success=='Proceed' I want to add value of login_success variable in the column. There is a way to do it using properties and properties_as_columns but I am not able to figure out how to do that. Please help

QApplication instance/qtbot fixture causes travis-ci to abort and core dump

你说的曾经没有我的故事 提交于 2020-04-13 16:52:31
问题 Working on understanding how to go about automated unit testing for PySide2-based applications. However, whenever I attempt to initialize a QApplication instance within the tests, be it through PySide2 itself or through pytest-qt 's qtbot fixture, travis-ci aborts the test. It works locally, however. I've attempted using the qtbot and qapp fixtures from pytest-qt , trying different travis-ci distros like xenial and trusty , as well as including the pytest-xvfb plugin as I've seen recommended

QApplication instance/qtbot fixture causes travis-ci to abort and core dump

你离开我真会死。 提交于 2020-04-13 16:51:28
问题 Working on understanding how to go about automated unit testing for PySide2-based applications. However, whenever I attempt to initialize a QApplication instance within the tests, be it through PySide2 itself or through pytest-qt 's qtbot fixture, travis-ci aborts the test. It works locally, however. I've attempted using the qtbot and qapp fixtures from pytest-qt , trying different travis-ci distros like xenial and trusty , as well as including the pytest-xvfb plugin as I've seen recommended

trying to make paths work - attempted relative import beyond top-level package

那年仲夏 提交于 2020-04-12 16:10:21
问题 I can't make this work.. My structure is: program_name/ __init__.py setup.py src/ __init__.py Process/ __init__.py thefile.py tests/ __init__.py thetest.py thetest.py: from ..src.Process.thefile.py import sth Running: pytest ./tests/thetest.py from program_name gives : ValueError: attempted relative import beyond top-level package I tried also other approaches but i am receiving various errors. But I would expect for the above to work. 回答1: ValueError: Attempted relative import in non-package