pytest

执行用例遇到错误停止

青春壹個敷衍的年華 提交于 2019-11-30 06:19:27
• 场景: • 正常全部执行完成后才能停止,如果想遇到错误时停止测试: -x;也可以当用例错误个数n达到指定数量时,停止测试:- - maxfail=n • 执行: • pytest -x -v -s 文件名.py      ------- -x是遇到错误就停止 • pytest -x -v -s 文件名.py —maxfail=2  ------- --maxfail=2 是遇到两个错误就停止 来源: https://www.cnblogs.com/QaStudy/p/11567079.html

pytest_参数化之3*3

怎甘沉沦 提交于 2019-11-30 06:07:54
import pytesttest_user_data1=[{'user':'linda','password':'888888'}, {'user':'servenruby','password':'123456'}, {'user':'test01','password':''}]test_user_data2=[{'q':'中国平安','count':3,'page':1}, {'q':'阿里巴巴','count':2,'page':2}, {'q':'pdd','count':3,'page':1}]@pytest.fixture(scope='module')def login_r(request): #这是接受不了输入的参数,接收一个参数 user=request.param['user'] pwd=request.param['password'] print('\n用户名:%s,密码:%s'%(user,pwd))@pytest.fixture(scope='module')def query_param(request): q=request.param['q'] count=request.param['count'] page=request.param['page'] print('查询的搜索词%s'%q) return request.param

pytest_参数化3

老子叫甜甜 提交于 2019-11-30 06:07:34
import pytesttest_user_data=[ {'user':'linda','password':'8888'}, {'user':'servenruby','password':'123456'}, {'user':'test01','password':''}]@pytest.fixture(scope='module')def login_r(request): #可以通过dict形式,虽然传递一个参数,但通过key的方式可以达到累死传入多个参数的效果 user=request.param['user'] pwd=request.param['password'] print('\n打开首页准备登陆,登陆用户%s,密码%s'%(user,pwd)) if pwd: return True else: return False#这是pytest参数化驱动,indeirect=True是把login_r当作函数去执行@pytest.mark.parametrize('login_r',test_user_data,indirect=True)def test_cart(login_r): #登陆用例 a=login_r print('测试用例中login_r的返回值%s'%a) assert a,'失败原因,密码为空' 开首页准备登陆,登陆用户linda

pytest -> How to use fixture return value in test method under a class

给你一囗甜甜゛ 提交于 2019-11-30 05:24:28
I have a fixture that returns a value like this: import pytest @pytest.yield_fixture(scope="module") def oneTimeSetUp(browser): print("Running one time setUp") if browser == 'firefox': driver = webdriver.Firefox() print("Running tests on FF") else: driver = webdriver.Chrome() print("Running tests on chrome") yield driver print("Running one time tearDown") This fixture gets the browser value from another fixture which is reading the command line option. Then I have a test class where I have more than one test methods and they all want to consume the same returned value driver to proceed the

pytest fixtures in a separate directory

纵饮孤独 提交于 2019-11-30 04:39:59
问题 I'm looking to create a pytest structure where I can separate the fixtures from the tests completely. The reason for this separation is that I want to include the fixtures directory as an external item in subversion and share it between multiple projects. tree of desired structure project | conftest.py | +---fixtures | __init__.py | conftest.py | fixture_cifs.py | fixture_ftp.py | fixture_service.py | \---tests | test_sometest1.py | test_sometest2.py | \---configurations sometest1.conf

Printing test execution times and pinning down slow tests with py.test

人盡茶涼 提交于 2019-11-30 04:35:52
I am running unit tests on a CI server using py.test. Tests use external resources fetched over network. Sometimes test runner takes too long, causing test runner to be aborted. I cannot repeat the issues locally. Is there a way to make py.test print out execution times of (slow) test, so pinning down problematic tests become easier? I'm not sure this will solve your problem, but you can pass --durations=N to print the slowest N tests after the test suite finishes. You can pass the number with --durations pytest --durations=0 — Show all times for tests and setup and teardown pytest --durations

How and where does py.test find fixtures

谁说我不能喝 提交于 2019-11-30 04:15:53
Where and how does py.test look for fixtures? I have the same code in 2 files in the same folder. When I delete conftest.py, cmdopt cannot be found running test_conf.py (also in same folder. Why is sonoftest.py not searched? # content of test_sample.py def test_answer(cmdopt): if cmdopt == "type1": print ("first") elif cmdopt == "type2": print ("second") assert 0 # to see what was printed content of conftest.py import pytest def pytest_addoption(parser): parser.addoption("--cmdopt", action="store", default="type1", help="my option: type1 or type2") @pytest.fixture def cmdopt(request): return

Create and import helper functions in tests without creating packages in test directory using py.test

风格不统一 提交于 2019-11-30 04:14:34
Question How can I import helper functions in test files without creating packages in the test directory? Context I'd like to create a test helper function that I can import in several tests. Say, something like this: # In common_file.py def assert_a_general_property_between(x, y): # test a specific relationship between x and y assert ... # In test/my_test.py def test_something_with(x): some_value = some_function_of_(x) assert_a_general_property_between(x, some_value) Using Python 3.5, with py.test 2.8.2 Current "solution" I'm currently doing this via importing a module inside my project's

How to pass environment variables to pytest

丶灬走出姿态 提交于 2019-11-30 04:12:25
Before i start executing the tests in my python project, i read some environment variables and set some variables with these values read. My tests will run on the desired environment based on these values read. for eg: Let's say the environment variables are called "ENV_NAME" and "ENV_NUMBER" Now, I would like to run the tests using py.test If i hard code these environment variables, for eg: ENV_NAME = 'staging', ENV_NUMBER = '5' in my code and then run the tests by executing the py.test command at the root of the project directory, all the tests run successfully. But, i don't want to hardcode

How to test single file under pytest

本秂侑毒 提交于 2019-11-30 04:12:12
How do you test a single file in pytest? I could only find ignore options and no "test this file only" option in the docs. Preferably this would work on the command line instead of setup.cfg , as I would like to run different file tests in the ide. The entire suite takes too long. Bryant simply run py.test with the path to the file something like py.test tests/unit/some_test_file.py This is pretty simple: $ pytest -v /path/to/test_file.py The -v flag is to increase verbosity. If you want to run a specific test within that file: $ pytest -v /path/to/test_file.py::test_name If you want to run