pytest

pytest_demo_实战2_fixture应用

三世轮回 提交于 2019-12-02 03:07:32
1、py文件所在文件夹下创建 __init__.py 文件 2、文件夹目录下:创建conftest.py import pytest # @pytest.fixture() 里面没有参数,那么默认scope="function", 也就是此时的级别 function,针对函数有效 """ firture相对于setup和teardown来说应该有以下几点优势 命名方式灵活,不局限于setup和teardown这几个命名 conftest.py 配置里可以实现数据共享,不需要import就能自动找到一些配置 scope="module" 可以实现多个.py跨文件共享前置 scope="session" 以实现多个.py跨文件使用一个session来完成多个用例 """ @pytest.fixture() def test(): print("pytest测试") 3、创建 test_02.py """ 小说网站 """ import pytest import requests import urllib3 from lxml import etree urllib3.disable_warnings() class Test: s = requests.session() uri = "https://so.88dush.com" def test_01(self, test):

How can pytest-cov report coverage of python code that is executed as a result of pexpect.spawn?

﹥>﹥吖頭↗ 提交于 2019-12-02 02:16:09
问题 I have a Python project that uses pytest-cov for unit testing and code coverage measurement. The directory structure for my project is: rift-python +- rift # The package under test | +- __init__.py | +- __main__.py | +- cli_listen_handler.py | +- cli_session_handler.py | +- table.py | +- ...lots more... +- tests # The tests | +- test_table.py | +- test_sys_2n_l0_l1.py | +- ...more... +- README.md +- .travis.yml +- ... I use Travis to run pytest --cov=rift tests for every checkin, and I use

How can I ensure tests with a marker are only run if explicitly asked in pytest?

青春壹個敷衍的年華 提交于 2019-12-02 02:09:16
问题 I have some tests I marked with an appropriate marker. If I run pytest, by default they run, but I would like to skip them by default. The only option I know is to explicitly say "not marker" at pytest invocation, but I would like them not to run by default unless the marker is explicitly asked at command line. 回答1: A slight modification of the example in Control skipping of tests according to command line option: # conftest.py import pytest def pytest_collection_modifyitems(config, items):

Limit the number of test cases to be executed in pytest

孤街浪徒 提交于 2019-12-02 01:54:17
A little background I am executing my test cases with Jenkins, I am doing a little POC with Jenkins right now. And, in my case, there are 500+ test cases which takes an hour to execute. I want only one test case to be executed just to know I didn't make any mistakes while doing my Jenkins POC. Is there a way to limit number of test case to be executed ? something like.. pytest -vv --limit 1 or using conftest.py? You can limit the amount of tests in many ways. For example, you can execute a single test by passing its full name as parameter: $ pytest tests/test_spam.py::TestEggs::test_bacon will

Is it possible to parametrize a test with fixtures?

大兔子大兔子 提交于 2019-12-02 01:35:43
I would like to pass @pytest.mark.parametrize not particular values but fixtures. Like so. Given a conftest with: @pytest.fixture def name1(): return 'foo' @pytest.fixture def name2(): return 'bar' within my test.py this works of course: @pytest.mark.parametrize('name', ['foo', 'bar', 'baz']) def test_name(name): print(name) This does not: @pytest.mark.parametrize('name', [name1, name2]) def test_name(name): print(name) I am aware that in this trivial case I could just create one name fixture and parametrize the fixture instead, but there are cases were this is not deireable. One way around

Is it possible to run tear down fixture only after all params runs?

隐身守侯 提交于 2019-12-02 00:52:15
For instance if you have: @pytest.mark.parametrize('lang', ["EN", "FR"]) def test_whats_hot_quick_links_are_displayed(self, lang): # Do something here and i have this teardown fixture in conftest: @pytest.fixture(scope='function', autouse=True) def teardown_function(request): def execute_at_the_end(): logging.info("Ending Test Case...") database.clear() request.addfinalizer(execute_at_the_end) So how can i make the teardown function execute only after both EN and FR test runs are executed instead of having this run after each param run? For this behaviour I use scope=class and wraps my test

Output ASCII art to console on succesfull pytest run

杀马特。学长 韩版系。学妹 提交于 2019-12-02 00:07:31
I'm using pytest to run tests in Django project. I'm using pytest.ini where DJANGO_SETTINGS_MODULE is defined, so I run tests with just: pytest Now, I want to add some ASCII art to the console output if the test run is successful. I know I can do: pytest && cat ascii_art.txt But I want to hide the ASCII art to config or somewhere else so that I keep running tests with just pytest . I don't see any pytest config option I can use. Any other ideas how this could be done? There are lots of places where you can print your own stuff in pytest ; select an appropriate hook from the hooks list and

pytest_demo_实战1

僤鯓⒐⒋嵵緔 提交于 2019-12-01 23:35:26
1、根目录配置 pytest.ini [pytest] addopts = -p no:warnings 2、更改运行手势,系统配置 file -> setting -> Tools -> Python Integrated Tools > 项目名称 -> Default test runner > 选择 py.test 3、创建test_01.py demo文件 """ 小说网站 """ import pytest import requestsclass Test: s = requests.session() uri = "https://www.zhihu.com" def test_01(self): url = "{}/signin?next=%2F".format(self.uri) dataAll = self.s.get(url) print(dataAll.content) if __name__ == '__main__': pytest.main(["-q", "test_01.py"]) 4、右键run.py ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.6

How can pytest-cov report coverage of python code that is executed as a result of pexpect.spawn?

流过昼夜 提交于 2019-12-01 22:25:45
I have a Python project that uses pytest-cov for unit testing and code coverage measurement. The directory structure for my project is: rift-python +- rift # The package under test | +- __init__.py | +- __main__.py | +- cli_listen_handler.py | +- cli_session_handler.py | +- table.py | +- ...lots more... +- tests # The tests | +- test_table.py | +- test_sys_2n_l0_l1.py | +- ...more... +- README.md +- .travis.yml +- ... I use Travis to run pytest --cov=rift tests for every checkin, and I use codecov to view the code coverage results. The package under test offer a command line interface (CLI)

How to test a class' inherited methods in pytest

被刻印的时光 ゝ 提交于 2019-12-01 21:31:31
问题 house.py : class House: def is_habitable(self): return True def is_on_the_ground(self): return True conftest.py : import pytest from house import House @pytest.fixture(scope='class') def house(): return House() test_house.py : class TestHouse: def test_habitability(self, house): assert house.is_habitable() def test_groundedness(self, house): assert house.is_on_the_ground() Up to that point, everything is being tested. Now I add a subclass and override a method in house.py : class House: def