pytest

How do I create a serializer that reuses a unique key of my model?

牧云@^-^@ 提交于 2021-02-07 10:36:56
问题 I'm using Python 3.7, Django 2.2, the Django rest framework, and pytest. I have the following model, in which I want to re-use an existing model if it exists by its unique key ... class CoopTypeManager(models.Manager): def get_by_natural_key(self, name): return self.get_or_create(name=name)[0] class CoopType(models.Model): name = models.CharField(max_length=200, null=False, unique=True) objects = CoopTypeManager() Then I have created the below serializer to generate this model from REST data

In pytest, how can I access the parameters passed to a test?

非 Y 不嫁゛ 提交于 2021-02-07 03:21:54
问题 In pytest, I can pass parameters to test (using fixtures or the decorator @pytest.fixture(params=list-of-params) ). When the tests are done, if a test fails, the parameter that was passed is shown on the results, as in TestCheckoutPage.test_address_edit[True] or False if it was false. How can I access those parameters and add them to a finalizer? request.param doesn't seem to work, even though that is how you would get the parameter when making a fixture: @pytest.fixture(params=[True, False])

Python Testing - Reset all mocks?

青春壹個敷衍的年華 提交于 2021-02-06 11:27:33
问题 When doing unit-testing with Python / PyTest, if you do you not have patch decorators or with patch blocks throughout your code, is there a way to reset all mocks at the end of every file / module to avoid inter-file test pollution? It seems like something that is mocked in one Python test file remains mocked in other file with the same return value, which means my mocks are persisting between tests and files (when a patch decorator or with patch block is NOT used). Is there any way around

Python Testing - Reset all mocks?

北战南征 提交于 2021-02-06 11:26:46
问题 When doing unit-testing with Python / PyTest, if you do you not have patch decorators or with patch blocks throughout your code, is there a way to reset all mocks at the end of every file / module to avoid inter-file test pollution? It seems like something that is mocked in one Python test file remains mocked in other file with the same return value, which means my mocks are persisting between tests and files (when a patch decorator or with patch block is NOT used). Is there any way around

Python Testing - Reset all mocks?

邮差的信 提交于 2021-02-06 11:25:12
问题 When doing unit-testing with Python / PyTest, if you do you not have patch decorators or with patch blocks throughout your code, is there a way to reset all mocks at the end of every file / module to avoid inter-file test pollution? It seems like something that is mocked in one Python test file remains mocked in other file with the same return value, which means my mocks are persisting between tests and files (when a patch decorator or with patch block is NOT used). Is there any way around

Python Testing - Reset all mocks?

点点圈 提交于 2021-02-06 11:25:12
问题 When doing unit-testing with Python / PyTest, if you do you not have patch decorators or with patch blocks throughout your code, is there a way to reset all mocks at the end of every file / module to avoid inter-file test pollution? It seems like something that is mocked in one Python test file remains mocked in other file with the same return value, which means my mocks are persisting between tests and files (when a patch decorator or with patch block is NOT used). Is there any way around

How do I get PyCharm to show entire error diffs from pytest?

ⅰ亾dé卋堺 提交于 2021-02-06 09:42:05
问题 I am using Pycharm to run my pytest unit tests. I am testing a REST API, so I often have to validate blocks of JSON. When a test fails, I'll see something like this: FAILED test_document_api.py:0 (test_create_documents) {'items': [{'i...ages': 1, ...} != {'items': [{'...ages': 1, ...} Expected :{'items': [{'...ages': 1, ...} Actual :{'items': [{'i...ages': 1, ...} <Click to see difference> When I click on the "Click to see difference" link, most of the difference is converted to points of

pytest自动化5:pytest-html插件--生成html报告

ⅰ亾dé卋堺 提交于 2021-01-30 02:15:34
前言:pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告。兼容Python 2.7,3.6 pytest-html 1. pip 安装: pip install pytest-html 2. 执行方法: pytest (-q 指定py脚本文件) --html = report.html【目标html名称,直接写名称是在当前目录下,也可定义目录下的文件夹:./ xx文件夹/ xx.html】 随便找一个脚本执行实例看下: 好了,在demo1文件夹下可以找到report.html,打开就可以看到报告了: 3. 失败重试 失败重跑需要依赖pytest -rerunfailures插件,使用pip安装即可。 pip安装命令:pip install pytest-rerunfailures 执行命令:pytest xx.py --reruns 1 --html=report2.html --self-contained-html 【--self-contained-html加这个是为了生成的html文件自带css样式】 从报告可以看出,rerun 一次。 来源: oschina 链接: https://my.oschina.net/u/4312735/blog/3615440

pytest 失败重跑截图

爷,独闯天下 提交于 2021-01-30 02:10:13
1.环境准备 /*@param: 作者:流浪的python Date:2019/01/19 env:python 3.7(由于3.0-3.5以下部分pytest可能有部分兼容问题安装建议2.7-2.9,3.5-最新) pip install pytest专属 pytest框架包 pip install pytest-html pytest自己专属报告包 pip install pytest-rerunfailures 失败重跑包也是pytest专属 并发的也可以安下,利用多cpu运行调高用例执行速度 python -m pip install xdist 2.用例配置conftest.py编写,这个文件名必须是这个,官网指定的,pytest运行前会自己寻找这个文件,如果有会按里面配置跑没有就默认按用例设置跑,conftest.py仅对同目录,同目录下文件生效,超出按自己的conftest.py配置,每个包可以有自己的conftest.py 目录结构: https://docs.pytest.org/en/latest/pythonpath.html#test-modules-conftest-py-files-inside-packages 参考以上链接官网解释 这里我给出我的结构供参考: project/ |- __init__.py |- conftest.py |- bar/

Add a description of what the test does using parametrize of Pytest

你。 提交于 2021-01-29 22:37:11
问题 It is possible to add a description in parametrize of what a test is testing for when one of them fails, know quickly why the test is failing. Sometimes you don't know why a test fails (you have to look in the code). With a description for each test, you could know. For example. @pytest.mark.parametrize( "num1, num2, expect", [ (2, 2, 4), # This test verifies that 2+2 = 4. ]) def test_sum(num1, num2, expect): calc = Calc() response = calc.sum(num1, num2) assert expect == response If the test