pytest

pytest + allure 生成测试报告

断了今生、忘了曾经 提交于 2019-11-28 00:56:08
pytest测试样例规则: 测试文件以test_开头(以_test结尾也可以) 测试类以Test开头,并且不能带有 init 方法 测试函数以test_开头 断言使用基本的assert即可 ubuntu 安装allure sudo apt-add-repository ppa:qameta/allure sudo apt-get update sudo apt-get install allure mac安装allure: brew install allure 源码安装参考地址: https://bintray.com/qameta/generic/allure2 安装pytest以及allure包: pip3 install pytest #不使用这个pip3 install pytest-allure-adaptor,用下面那个 pip3 install allure-pytest 创建一个用例 test_one.py: class TestClassOne(object): def test_one(self): x = "this" assert 't'in x def test_two(self): x = "hello" assert hasattr(x, 'check') class TestClassTwo(object): def test_one(self):

pytest以函数形式形成测试用例

ε祈祈猫儿з 提交于 2019-11-28 00:32:52
#coding=utf-8 from __future__ import print_function #开始执行该文件时,该函数执行 def setup_module(module): print('\nsetup_module()') #结束执行该文件时,该函数执行 def teardown_module(module): print('teardown_module()') #单元测试函数执行之前该函数执行 def setup_function(function): print('\nsetup_function()') #单元测试函数执行之后该函数执行 def teardown_function(function): print('\nteardown_function()') #case1 def test_1(): print('- test_1()') #case2 def test_2(): print('- test_2()') 输出 bogon:test macname$ pytest test.py -s ============================= test session starts ============================== platform darwin -- Python 3.6.3, pytest-5.1.0,

How can I limit the maximum running time for a unit test?

我与影子孤独终老i 提交于 2019-11-27 22:57:36
问题 I am currently running some unit tests that might either take a long time before failing or run indefinitely. In a successful test run they will always complete within a certain amount of time. Is it possible to create a pytest unit test that will fail if it does not complete within a certain amount of time? 回答1: you can install the pytest-timeout plugin and then mark your test functions with a timeout in seconds. @pytest.mark.timeout(300) def test_foo(): pass Look at the plugin download and

How to parametrize a Pytest fixture

…衆ロ難τιáo~ 提交于 2019-11-27 22:44:06
Consider the following Pytest: import pytest class TimeLine(object): instances = [0, 1, 2] @pytest.fixture def timeline(): return TimeLine() def test_timeline(timeline): for instance in timeline.instances: assert instance % 2 == 0 if __name__ == "__main__": pytest.main([__file__]) The test test_timeline uses a Pytest fixture, timeline , which itself has the attribute instances . This attribute is iterated over in the test, so that the test only passes if the assertion holds for every instance in timeline.instances . What I actually would like to do, however, is to generate 3 tests, 2 of which

Importing correctly with pytest

♀尐吖头ヾ 提交于 2019-11-27 20:53:09
问题 I just got set up to use pytest with Python 2.6. It has worked well so far with the exception of handling "import" statements: I can't seem to get pytest to respond to imports in the same way that my program does. My directory structure is as follows: src/ main.py util.py test/ test_util.py geom/ vector.py region.py test/ test_vector.py test_region.py To run, I call python main.py from src/. In main.py, I import both vector and region with from geom.region import Region from geom.vector

Test discovery failure when tests in different directories are called the same

依然范特西╮ 提交于 2019-11-27 20:23:24
问题 Using py.test, two tests called the same in different directory causes py.test to fail. Why is that? How can I change this without renaming all the tests? To duplicate do: ; cd /var/tmp/my_test_module ; mkdir -p ook/test ; mkdir -p eek/test ; touch ook/test/test_proxy.py ; touch eek/test/test_proxy.py ; py.test ============================= test session starts ============================== platform linux2 -- Python 2.7.3 -- pytest-2.2.4 collected 0 items / 1 errors ==========================

How to add a screenshot to allure report with python?

烂漫一生 提交于 2019-11-27 18:25:44
问题 I have this code: # coding: utf-8 from selenium import webdriver import pytest import allure @pytest.yield_fixture(scope='session') def driver(): _driver = webdriver.PhantomJS() yield _driver _driver.quit() def test_ya(driver): with allure.step('open ya.ru and take screenshot'): driver.get('http://ya.ru/') allure.attach('screenshot', driver.get_screenshot_as_png(), type='png') and I try to take a screenshot and save it to allure report, after execution I have: > with self._attachfile("%s

Logging within py.test tests

非 Y 不嫁゛ 提交于 2019-11-27 17:47:57
I would like to put some logging statements within test function to examine some state variables. I have the following code snippet: import pytest,os import logging logging.basicConfig(level=logging.DEBUG) mylogger = logging.getLogger() ############################################################################# def setup_module(module): ''' Setup for the entire module ''' mylogger.info('Inside Setup') # Do the actual setup stuff here pass def setup_function(func): ''' Setup for test functions ''' if func == test_one: mylogger.info(' Hurray !!') def test_one(): ''' Test One ''' mylogger.info(

Python - Using pytest to skip test unless specified

梦想的初衷 提交于 2019-11-27 16:57:09
问题 Background I have am using pytest to test a web scraper that pushes the data to a database. The class only pulls the html and pushes the html to a database to be parsed later. Most of my tests use dummy data to represent the html. Question I want to do a test where a webpage from the website is scraped but I want the test to be automatically turned off unless specified. A similar scenario could be if you have an expensive or time consuming test that you do not want to always run. Expected

pytest_用例运行级别_class级

ぃ、小莉子 提交于 2019-11-27 16:14:16
''' 模块级(setup_module/teardown_module)开始于模块始末, 全局的在类中不起作用 类级(setup_class/teardown_class)只在类中前后运行一次(在 类中) 方法级(setup_method/teardown_method)开始于方法始末 (在类中) 函数级(setup_function/teardown_function只对函数用例生 效(在类中不生效) setup_function teardown_function ''' def setup_function(): print() print("setup_function:每个用例前开始执行") def teardown_function(): print("teardown_function:没个用例后开始执行") import pytest def setup_module(): """ 这是一个module级别的setup,它会在本module(test_fixt_class.py)里 所有test执行之前,被调用一次。 注意,它是直接定义为一个module里的函数""" print() print("-------------- setup before module --------------") def teardown_module(): """