pytest

pytest_用例运行级别_函数级

眉间皱痕 提交于 2019-11-27 16:12:29
'''  函数级(setup_function/teardown_function只对函数用例生 效(不在类中)在类中是用该方法不生效 ''' import pytest def setup_module(): """ 这是一个module级别的setup,它会在本module(test_fixt_function.py)里 所有test执行之前,被调用一次。 注意,它是直接定义为一个module里的函数""" print() print("-------------- setup before module --------------") def teardown_module(): """ 这是一个module级别的teardown,它会在本module(test_fixt_function.py)里 所有test执行完成之后,被调用一次。 注意,它是直接定义为一个module里的函数""" print("-------------- teardown after module --------------") def setup_function(): print() print("setup_function:每个用例前开始执行") def teardown_function(): print("teardown_function:没个用例后开始执行") def

pytest_用例运行级别_模块级

孤街醉人 提交于 2019-11-27 16:11:29
''' pytest 参数说明 https://www.jianshu.com/p/7a7432340f02 -x test_fixt_model.py 遇到错误时,停止运行 用-v运行(-v显示运行的函数)py.test –v test_fixt_model.py, 用例设计原则 文件名以 test_*.py 文件和*_test.py 以 test_开头的函数 以 Test 开头的类 以 test_开头的方法 所有的包 pakege 必项要有__init__.py 文件 用例运行级别  模块级(setup_module/teardown_module)开始于模块始末, 全局的在类中不起作用  函数级(setup_function/teardown_function只对函数用例生 效(不在类中)  类级(setup_class/teardown_class)只在类中前后运行一次(在 类中)  方法级(setup_method/teardown_method)开始于方法始末 (在类中)  类里面的(setup/teardown)运行在调用方法的前后 ''' import pytest def setup_module(): """ 这是一个module级别的setup,它会在本module(test_fixt_model.py)里 所有test执行之前,被调用一次。 注意

pytest跳过指定的测试或模块

最后都变了- 提交于 2019-11-27 16:07:09
参考 pytest常见用法 , pytest官方文档 1.运行带指定标记的测试 @pytest.mark.tags ,这里的tags可以自定义 命令行执行:pytest -v -m 'tags' 2.跳过指定的测试 @pytest.mark.skip(reason="过期")#跳过该测试 def test_app_logic(): ''' 用例描述:逻辑测试 ''' print('逻辑测试') time.sleep(1) assert 1==1 3.跳过指定的模块 实际测试时,当 @pytest.importorskip("模块名",minversion="1.5")这个装饰器在哪个模块,哪个模块就会被跳过,模块名随便写都没影响。 @pytest.importorskip("test_ltcs",minversion="1.5") @allure.step('检查UI名:{0}打开了') def ui_check(tips): return tips 4.条件跳过指定的用例 @pytest.mark.skipif(条件) 来源: https://www.cnblogs.com/sc912/p/11369651.html

pytest常用命令参数

会有一股神秘感。 提交于 2019-11-27 15:53:26
pytest 参数 1.参数:-s 运行过程中执行print打印函数:pytest -s,以下两个输出 上边带参数,下边不带 2.参数: --collect-only 收集将要执行的用例,但不会执行用例:pytest --collcet-onty 3.参数:-k args(关键字args:可以是py文件名,也可以是函数名,若py文件名和函数均包含 则需要严格指定 xx.py 以运行py文件) 运行包含关键词的用例:pytest -k "install",如下图: 4.参数:-x 用例运行失败则立即停止执行 5.参数:--maxfail=num 用例运行时 允许的最大失败次数,超过则立即停止,pytest --maxfail=3 6.参数:--tb=选项(选项:'auto', 'long', 'short', 'no', 'line', 'native') 用例运行失败时,展示错误的详细程度 7.参数:-l 或--showlocals 用例运行失败时,打印相关的局部变量,pytest -l 8.参数:-v 或 -q 打印用例执行的详细/简略过程,pytest -v ,pytest -q 9.参数:--lf / --last-failed 只执行上次执行失败的测试 10.参数:--ff / --failed-first 先执行完上次失败的测试后,再执行上次正常的测试 11.参数:-

Selenium - visibility_of_element_located: __init__() takes exactly 2 arguments (3 given)

情到浓时终转凉″ 提交于 2019-11-27 14:50:44
I am getting this error in my test code that uses Selenium Python Bindings: > twitter_campaigns = wait.until(EC.visibility_of_element_located(By.CSS_SELECTOR, TWITTER_CAMPAIGNS)) E TypeError: __init__() takes exactly 2 arguments (3 given) And this is what Im executing: class TestTwitter(TestLogin, TestBuying): def setup(self, timeout=10): self.driver = webdriver.Firefox() self.driver.get(BASEURL) self.driver.implicitly_wait(timeout) def test_campaigns_loaded(self, timeout=10): self.signin_action() self.view_twitter_dashboard() self.select_brand() wait = WebDriverWait(self.driver, timeout)

pytest using fixtures as arguments in parametrize

你说的曾经没有我的故事 提交于 2019-11-27 14:33:24
问题 I would like to use fixtures as arguments of pytest.mark.parametrize or something that would have the same results. For example: import pytest import my_package @pytest.fixture def dir1_fixture(): return '/dir1' @pytest.fixture def dir2_fixture(): return '/dir2' @pytest.parametrize('dirname, expected', [(dir1_fixture, 'expected1'), (dir2_fixture, 'expected2')] def test_directory_command(dirname, expected): result = my_package.directory_command(dirname) assert result == expected The problem

Py.test No module named *

我只是一个虾纸丫 提交于 2019-11-27 14:27:40
问题 I have a folder structure like this App --App --app.py --Docs --Tests --test_app.py In my test_app.py file , I have a line to import my app module. When I run py.test on the root folder, I get this error about no module named app. How should I configure this? 回答1: So you are running py.test from /App . Are you sure /App/App is in your $PYTHONPATH ? If it's not, code that tries to import app will fail with such a message. EDIT0: including the info from my comment below, for completeness. An

pytest参数化的两种方式

大兔子大兔子 提交于 2019-11-27 13:58:04
1、传统方式 1 import requests,pytest 2 from Learning.variable import * 3 4 # 定义变量 5 #url = "https://www.baidu.com" 6 7 class TestClass(object): 8 global url #在此获取全局变量,并将其设置为TestClass类的全局变量 9 def setup_class(self): 10 print("start...") 11 12 def test_get(self): 13 #global url #在此获取全局变量,并将其设置为test_get方法内的全局变量 14 res = requests.get(url=url) 15 assert res.status_code == 200 16 17 18 if __name__ == '__main__': 19 pytest.main() 2、pytest推荐模式,即conftest测试数据共享 2.1、在function中使用 # content of test01.py import pytest,requests #将conftest中的com_variable方法传入用例中,不需要导入即可使用 def test_getBaidu(com_variable): a=requests

How to pass environment variables to pytest

橙三吉。 提交于 2019-11-27 13:25:53
问题 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

Run code before and after each test in py.test?

核能气质少年 提交于 2019-11-27 13:25:13
问题 I want to run additional setup and teardown checks before and after each test in my test suite. I've looked at fixtures but not sure on whether they are the correct approach. I need to run the setup code prior to each test and I need to run the teardown checks after each test. My use-case is checking for code that doesn't cleanup correctly: it leaves temporary files. In my setup I will check the files and in the teardown I also check the files. If there are extra files I want the test to fail