pytest

Pytest override fixture scope

廉价感情. 提交于 2021-02-10 05:35:27
问题 I pytest, we declare the fixture scope (function, module, class, session) at the fixture definition. Is it possible to override that scope at the usage of it in a test? So that I can have a default function-scope fixture, but some tests which are using it use it as module-scope? My concrete scenario is that I'm using parametrized tests, but I want my fixture to be used once over all parameters. Otherwise, the fixture is good to have function scope for all other tests. 来源: https:/

Pytest override fixture scope

有些话、适合烂在心里 提交于 2021-02-10 05:35:11
问题 I pytest, we declare the fixture scope (function, module, class, session) at the fixture definition. Is it possible to override that scope at the usage of it in a test? So that I can have a default function-scope fixture, but some tests which are using it use it as module-scope? My concrete scenario is that I'm using parametrized tests, but I want my fixture to be used once over all parameters. Otherwise, the fixture is good to have function scope for all other tests. 来源: https:/

02、pytest常用命令行选项

萝らか妹 提交于 2021-02-09 20:33:01
可以使用 -v 选项,显示测试的详细信息。 pytest -v demo.py =================================================== test session starts ==================================================== platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.4.0', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'bdd': '3.1.0', 'html': '1.20.0', 'metadata': '1.8.0'}} rootdir: /Users/zhouwanghua/Code/Leyan/robocop, inifile: pytest.ini

Provide default argument value for py.test fixture function

眉间皱痕 提交于 2021-02-09 10:25:02
问题 Is there a better way for me to provide default value for argument pytest.fixture function? I have a couple of testcases that requires to run fixture_func before testcase and I would like to use default value for argument in fixture if none of them is provided. The only code I can come up with is as follows. Any better way for me to do it? @pytest.fixture(scope="function") def fixture_func(self, request): argA = request.param['argA'] if request.param.get('argA', None) else 'default value for

Provide default argument value for py.test fixture function

大兔子大兔子 提交于 2021-02-09 10:12:57
问题 Is there a better way for me to provide default value for argument pytest.fixture function? I have a couple of testcases that requires to run fixture_func before testcase and I would like to use default value for argument in fixture if none of them is provided. The only code I can come up with is as follows. Any better way for me to do it? @pytest.fixture(scope="function") def fixture_func(self, request): argA = request.param['argA'] if request.param.get('argA', None) else 'default value for

What is a correct approach to manage test data using pytest?

半世苍凉 提交于 2021-02-09 08:59:49
问题 I need to create automated tests for several related apps and faced one problem with test data management between tests. The problem is that same data must be shared between several apps and/or different APIs. Now I have next structure with pytest that working good for me but I doubt that use test data managment in conftest.py is correct approach: Overall structure looks like: tests/ conftest.py app1/ conftest.py test_1.py test_2.py app2/ conftest.py test_1.py test_2.py test_data/ test_data

How to replace variable inside function in unittest- pytest

两盒软妹~` 提交于 2021-02-08 10:00:48
问题 I have a problem with replace variable inside method which i have to test, namely: def find_files(path): path_dir = os.listdir(path) ... and for needs of test I have to replace path_dir from real result of os.listdir to some test list i.e. ['whatever1.txt', 'whatever2.txt', 'whatever3.txt'] How to do it? BR, Damian 回答1: You can use mock.patch to set return value for your variable. For example with patch('os.listdir') as mocked_listdir: mocked_listdir().return_value = ['.', '..'] find_files

How to replace variable inside function in unittest- pytest

走远了吗. 提交于 2021-02-08 10:00:08
问题 I have a problem with replace variable inside method which i have to test, namely: def find_files(path): path_dir = os.listdir(path) ... and for needs of test I have to replace path_dir from real result of os.listdir to some test list i.e. ['whatever1.txt', 'whatever2.txt', 'whatever3.txt'] How to do it? BR, Damian 回答1: You can use mock.patch to set return value for your variable. For example with patch('os.listdir') as mocked_listdir: mocked_listdir().return_value = ['.', '..'] find_files

gitlab-ci.yml: 'script: -pytest cannot find any tests to check'

自作多情 提交于 2021-02-08 08:55:21
问题 I'm having trouble implementing a toy example that runs pytest within .gitlab-ci.yml gitlab_ci is a repo containing a single file test_hello.py gitlab_ci/ test_hello.py test_hello.py # test_hello.py import pytest def hello(): print("hello") def hello_test(): assert hello() == 'hello' .gitlab-ci.yml # .gitlab-ci.yml pytest: image: python:3.6 script: - apt-get update -q -y - pip install pytest - pytest # if this is removed, the job outputs 'Success' CI/CD terminal output $ pytest === test

How do I include screenshot in python pytest html report

我的未来我决定 提交于 2021-02-08 05:17:45
问题 When I hit the below url in browser "https://192.168.xx.xxx/Test/ScreenCapture" I get the screenshot of the device screen under test in the browser. How do I add the screenshot in my pytest html test report. Currently I am using below code which captures screen shot in the test directory specified. url = 'https://192.168.xx.xxx/Test/ScreenCapture' driver.get(url) driver.save_screenshot('/home/tests/screen.png') I am running my pytest with below command : py.test --html=report.html --self