pytest

pytest学习3-断言

不打扰是莪最后的温柔 提交于 2020-01-07 21:20:19
断言: 一个标准的用例都包含了断言,编写pytest自动化脚本的时候,也需要设置断言 pytest常用断言比较大小与是否相等、是否包含、验证boolean 例子一: 验证是否相等: import pytest def test_eq_1(): assert 1==1 class Test_cls: def test_eq_2(self): assert 2!=3 def test_eq_3(self): assert 4>3 def test_eq_4(self): assert 3<6 def test_eq_5(self): assert 3==4 if __name__ == '__main__': pytest.main(["-s","test_2.py"]) 例子二: 验证是否包含: import pytest def test_in_1(): assert 'a' in 'abc' def test_in_2(): assert 'c' in 'abd' if __name__ == '__main__': pytest.main(["-s","test_2.py"]) 例子三: 验证boolean import pytest def func(n): if n==10: return True else: return False def test_true_1():

pytest学习2-运行方式

心已入冬 提交于 2020-01-07 21:07:31
pytest常用运行方式 运行目录及子包下的所有用例: pytest 目录名 运行指定模块所有用例: pytest test_reg.py pytest test_reg.py::TestClass::test_method 运行指定模块指定类指定用例 运行名称包含指定表达式的用例:-k 表达式(支持and or not),如pytest -k "test_a and test_b" 运行指定标签(mark)的用例: -m 标签(支持and or not), 如pytest -m "apitest and level1" 遇到失败后停止:-x/--exitfirst 首次失败后退出(可用于保留出错现场) --maxfails=3 3次失败后退出,如下: pytest -x # 第1次失败后停止 pytest --maxfail=2 # 2次失败后停止 执行上次失败的用例 --lf/--last-failed 先执行上次失败的用例,再执行成功的用例 --ff/--failed-first 只收集用例,不执行 --collect-only 显示执行最慢的前N条用例:--durations=N 并行执行: -n 2 (需安装pytest-xdist) 常用的参数: 其它常用参数 -q: 安静模式, 不输出环境信息 -v: 丰富信息模式, 输出更详细的用例执行信息 -s:

pytest学习1-安装和入门

半腔热情 提交于 2020-01-07 20:54:36
一、安装pytest:   在命令行窗口下执行:   pip install -U pytest   检查安装的pytest版本:   pytest -v 二、运行第一个测试用例:    import pytestdef func(n): return n+1def test_answer(): assert 4==func(3)if __name__ == '__main__': pytest.main(["-q","test_1.py"])运行结果: 三、Pytest用例编写规则:    文件名以test_ .py文件和 _test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 来源: https://www.cnblogs.com/qixc/p/12163495.html

Mock exception raised from class method with side effect gives 'did not raise'

时光怂恿深爱的人放手 提交于 2020-01-07 07:59:09
问题 Note: This question is based on a previous question I asked but modified to be different according to this answer . Using side_effect, I am trying to raise a 'URLError' exception when a mock is called but I get a DID NOT RAISE error that I do not understand. I have a Query class with a class method make_request_and_get_response which can raise several exceptions. I am not catching the 'URLError' exception within the get_response_from_external_api method in main.py , so I should be able to

Python imports with __init__.py

元气小坏坏 提交于 2020-01-07 03:59:20
问题 No matter how I structure the imports in the code files and in the __init__.py files, I can't seem to get it right for executing the program and running the tests using pytest . How do I need to write the imports when my project structure looks like this: src/ __init__.py VocableFileWriter.py WordListHelper.py WordListReader.py XLDAttributeValueAdder.py exceptions/ __init__.py XMLInvalidException.py XMLParseException.py gui/ __init__.py GTKSignal.py XLDAttributeValueAdderWindow.py test/ _

Parametrizing tests depending of also parametrized values in pytest

徘徊边缘 提交于 2020-01-06 07:21:49
问题 I have a test where I have a setup method, that should receive a dataset , and a test function, that should run for each data in dataset Basically I would need something like: datasetA = [data1_a, data2_a, data3_a] datasetB = [data1_b, data2_b, data3_b] @pytest.fixture(autouse=True, scope="module", params=[datasetA, datasetB]) def setup(dataset): #do setup yield #finalize #dataset should be the same instantiated for the setup @pytest.mark.parametrize('data', [data for data in dataset]) def

How to test views with pytest whose views has LoginRequired and some specific user dependencies

馋奶兔 提交于 2020-01-06 07:14:15
问题 I am testing a view and while testing this I am getting this error self = <django.db.models.fields.AutoField: id>, value = '' def get_prep_value(self, value): from django.db.models.expressions import OuterRef value = super().get_prep_value(value) if value is None or isinstance(value, OuterRef): return value > return int(value) E ValueError: invalid literal for int() with base 10: '' /usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py:965: ValueError And I think I am

Python pytest: capturing stdout always fails

折月煮酒 提交于 2020-01-06 05:45:26
问题 I'm trying to capture output from stdout / print statements in pytest (Python version 3.6). This always fails: message = 'The meaning of life is not actually 42\n' def print_greeting(): """print 42 to stdout""" # write to stdout sys.stdout.write(message) # this fails # print to stdout print('Message again: ', message) # this fails too def test_printgreeting(capsys): """assert '42' was printed to stdout""" # capture stdout / stderr out, err = capsys.readouterr() print_greeting() # 42 should be

Can we print the command line arguments in setup_module() or setup_class in pytest?

六眼飞鱼酱① 提交于 2020-01-05 07:17:18
问题 I got to know how to pass command line parameters in pytest. I want to print or do some basic validation in my setup_class() or setup_module() against the variables passed as command line arguments. I am able to access these variables in test methods but not in setup_class or setup_module. Is this even possible? I want to do something like this... conftest.py def pytest_addoption(parser): parser.addoption('--user',action='store', help='email id Ex: abc@infoblox.com') test_1.py import pytest

Create automated tests for interactive shell based on Python's cmd module

给你一囗甜甜゛ 提交于 2020-01-04 04:13:24
问题 I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. I'd like to create more comprehensive tests that actually interact with the shell itself by simulating a user's input. For example, how could I test the following simulated session: bash$ console-app.py md:> show options Available Options: ------------------ HOST The IP address or hostname of the machine to