目前,可以标记测试,然后使用-m
参数运行它们(或不运行它们)。但是,仍然首先收集所有测试,然后才取消选择
在下面的示例中,仍然收集所有 8 个,然后运行 4 个并取消选择 4 个。
============================= test session starts ==============================
platform win32 -- Python 2.7.3 -- pytest-2.3.2 -- C:\Python27\python.exe
collecting ... collected 8 items
test_0001_login_logout.py:24: TestLoginLogout.test_login_page_ui PASSED
test_0001_login_logout.py:36: TestLoginLogout.test_login PASSED
test_0001_login_logout.py:45: TestLoginLogout.test_default_admin_has_users_folder_page_loaded_by_default PASSED
test_0001_login_logout.py:49: TestLoginLogout.test_logout PASSED
==================== 4 tests deselected by "-m 'undertest'" ====================
================== 4 passed, 4 deselected in 1199.28 seconds ===================
问题:是否可以完全不收集标记/未标记的测试?
问题是:
1)当数据库中已经有一些项目(比如我的设备)和它的代码时,我正在使用一些测试:
@pytest.mark.device
class Test1_Device_UI_UnSelected(SetupUser):
#get device from the database
device = Devices.get_device('t400-alex-win7')
@classmethod
@pytest.fixture(scope = "class", autouse = True)
def setup(self):
...
我运行测试明确排除和设备测试:py.test -m "not device"
但是,在收集过程中我得到了错误,因为device = Devices.get_device('t400-alex-win7')
仍在执行。
2) 一些测试被标记time_demanding
,因为大约有 400 个生成的测试。生成这些测试也需要时间。我从一般测试中排除了这些测试,但是它们是生成和收集的,然后被取消选择<-只是等待时间。
我知道 (1) 问题有一个解决方案 - 使用 pytest.fixtures 并将它们传递给测试,但是我真的很喜欢PyDev 提供的自动完成功能。
timedemanding
类是:
import pytest
#... other imports
def admin_rights_combinations(admin, containing = ["right"]):
'''
Generate all possible combinations of admin rights settings depending
on "containing" restriction
'''
rights = [right for right in admin.__dict__.iterkeys() if any(psbl_match in right for psbl_match in containing)]
total_list = []
l = []
for right in rights: #@UnusedVariable
l.append([True, False])
for st_of_values in itertools.product(*l):
total_list.append(dict(zip(rights, st_of_values)))
return total_list
@pytest.mark.timedemanding
class Test1_Admin_Rights_Access(SetupUser):
user = UserFactory.get_user("Admin Rights Test")
user.password = "RightsTest"
folder = GroupFolderFactory.get_folder("Folders->Admin Rights Test Folder")
group = GroupFolderFactory.get_group("Folders->Admin Rights Test Group")
admin = UserFactory.get_admin("Admin Rights Test")
@classmethod
@pytest.fixture(scope = "class", autouse = True)
def setup(self):
...
@pytest.mark.parametrize("settings", admin_rights_combinations(admin, containing=['right_read',
'right_manage_folders',
'right_manage_groups']))
def test_admin_rights_menus(self, base_url,settings):
'''
test combination of admin rights and pages that are displayed with
this rights. Also verify that menu's that are available can be opened
'''
正如你所看到的,当 pytest 点击@pytest.mark.parametrize
它时,它应该已经知道它在 Class with 中@pytest.mark.timedemanding
。但是,收集仍然发生。