如果我在一个 class 中有多个测试并且前面的测试失败,我如何让它跳过或退出 class 而不是测试剩余的测试?
If I have multiple tests in one class and a preceding test fails, how do I have it skip or exit the class instead of testing the remaining tests?
我正在使用 Python 与 Selenium 和单元测试。我将四个测试合而为一 class,因为它们都是相关的单元测试。如果前面的测试失败,我如何让它跳过下一个测试?我已经阅读了 unittest 的 skip 方法的所有文档,但 none 正是我所需要的。有没有办法告诉它退出 class?
这是我的代码目前的样子:
def test_dealer_search_id_contains(self):
try:
LoginPage.login(self, ULV.AA_USERNAME, ULV.AA_PASSWORD)
except TimeoutException:
add_test_result(3, 5, "This test failed attempting to login with the user's credentials.")
add_test_result(4, 2, 'This test is blocked due to failure of a preceding test.')
self.fail(msg='Dealer: Search - Test failure.')
def test_dealer_search_id_exact(self):
code for this test here
如何让它跳过剩余的测试或一起退出 class?
感谢您提供的任何帮助。
编辑:我正在使用 PyTest 作为我的 运行ner,在这些之后我确实有额外的测试需要继续 运行,只是不在这个 class 或 . py 文件。
编辑:所以在这种情况下的测试都取决于先前的测试通过,否则它会因为与上述测试相同的缺陷而失败。但是,我的主要困境是我不确定如何告诉它跳过这些测试并继续 PyTest 已选择的其余测试。我查看了 PyTest 的跳过处理,它与 unittest 几乎相同。如果不必要的话,我试图避免在测试失败的逻辑检查中编码,但看起来这可能是唯一的解决方案。
为什么不在测试中创建依赖项
例如:
def test_mainTest(): ...
@dependsOn(test_mainTest)
def test_dependantTest(): ...
def test_non_dependant_test(): ...
使用标志 -x
或 --exitfirst
在第一次出错或测试失败时立即退出。
或 --maxfail=num
在前 num 次失败或错误后退出。
http://pytest.org/latest/usage.html#stopping-after-the-first-or-n-failures
因此,由于测试设置的复杂情况,我最终不得不做的是,我将 Class 中的三个测试包围起来,如果第一个测试失败,这三个测试将失败,因为除了搜索数据。
例如:
class TestDealerSearchID(BaseSetup, LoginPage, DealerPage):
testSuiteResult = 0
def test_dealer_search_id_contains(self):
try:
LoginPage.login(self, ULV.AA_USERNAME, ULV.AA_PASSWORD)
except (Exception, TimeoutException):
add_test_result(3, 5, "This test failed attempting to login with the user's credentials.")
add_test_result(4, 2, 'This test is blocked due to the failure of a preceding test in the suite.')
add_test_result(5, 2, 'This test is blocked due to the failure of a preceding test in the suite.')
add_test_result(6, 2, 'This test is blocked due to the failure of a preceding test in the suite.')
TestDealerSearchID.testSuiteResult = 1
self.fail(msg='AAv4 - Dealer: Search - Test failure.')
def test_dealer_search_id_exact(self):
if TestDealerSearchID.testSuiteResult == 1:
TestDealerSearchID.skipTest(self, 'This test is blocked due to the failure of a preceding test in the suite.')
else:
pass
REST OF CODE HERE AND REMAINING TWO TESTS WITH SAME IF/ELSE STATEMENT.
这是我能想到的最好的解决方案,而且它完全符合需要。如果有人知道更好的方法,请不要犹豫发表评论或 post 一个答案。
谢谢,
杰伊·赖特
我想我找到了解决这个问题的方法。它在 py.test 框架中被称为 incremental testing
:https://pytest.org/latest/example/simple.html#incremental-testing-test-steps
我在本地进行了测试 - 效果非常好!
使用与 py.test 文档中相同的 conftest.py
。
略有不同的测试文件:
# content of test_incremental.py
import pytest
@pytest.mark.incremental
class TestUserHandling:
def test_login(self):
assert 1
def test_modification(self):
assert 0
def test_deletion(self):
assert 1
def test_foo(self):
assert 1
class TestAnotherStuff:
def test_normal(self):
assert 1
mac:[incremental_testing]: py.test -v test_incremental.py
============================================================================== test session starts ===============================================================================
platform darwin -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
cachedir: .cache
rootdir: /Users/dmitry.tokarev/Repos/playground/incremental_testing, inifile:
plugins: hidecaptured-0.1.2, instafail-0.3.0
collected 5 items
test_incremental.py::TestUserHandling::test_login PASSED
test_incremental.py::TestUserHandling::test_modification FAILED
test_incremental.py::TestUserHandling::test_deletion xfail
test_incremental.py::TestUserHandling::test_foo xfail
test_incremental.py::TestAnotherStuff::test_normal PASSED
==================================================================================== FAILURES ====================================================================================
_______________________________________________________________________ TestUserHandling.test_modification _______________________________________________________________________
self = <test_incremental.TestUserHandling instance at 0x103b7a5a8>
def test_modification(self):
> assert 0
E assert 0
test_incremental.py:10: AssertionError
================================================================= 1 failed, 2 passed, 2 xfailed in 0.02 seconds ==================================================================
我正在使用 Python 与 Selenium 和单元测试。我将四个测试合而为一 class,因为它们都是相关的单元测试。如果前面的测试失败,我如何让它跳过下一个测试?我已经阅读了 unittest 的 skip 方法的所有文档,但 none 正是我所需要的。有没有办法告诉它退出 class?
这是我的代码目前的样子:
def test_dealer_search_id_contains(self):
try:
LoginPage.login(self, ULV.AA_USERNAME, ULV.AA_PASSWORD)
except TimeoutException:
add_test_result(3, 5, "This test failed attempting to login with the user's credentials.")
add_test_result(4, 2, 'This test is blocked due to failure of a preceding test.')
self.fail(msg='Dealer: Search - Test failure.')
def test_dealer_search_id_exact(self):
code for this test here
如何让它跳过剩余的测试或一起退出 class?
感谢您提供的任何帮助。
编辑:我正在使用 PyTest 作为我的 运行ner,在这些之后我确实有额外的测试需要继续 运行,只是不在这个 class 或 . py 文件。
编辑:所以在这种情况下的测试都取决于先前的测试通过,否则它会因为与上述测试相同的缺陷而失败。但是,我的主要困境是我不确定如何告诉它跳过这些测试并继续 PyTest 已选择的其余测试。我查看了 PyTest 的跳过处理,它与 unittest 几乎相同。如果不必要的话,我试图避免在测试失败的逻辑检查中编码,但看起来这可能是唯一的解决方案。
为什么不在测试中创建依赖项 例如:
def test_mainTest(): ...
@dependsOn(test_mainTest)
def test_dependantTest(): ...
def test_non_dependant_test(): ...
使用标志 -x
或 --exitfirst
在第一次出错或测试失败时立即退出。
或 --maxfail=num
在前 num 次失败或错误后退出。
http://pytest.org/latest/usage.html#stopping-after-the-first-or-n-failures
因此,由于测试设置的复杂情况,我最终不得不做的是,我将 Class 中的三个测试包围起来,如果第一个测试失败,这三个测试将失败,因为除了搜索数据。
例如:
class TestDealerSearchID(BaseSetup, LoginPage, DealerPage):
testSuiteResult = 0
def test_dealer_search_id_contains(self):
try:
LoginPage.login(self, ULV.AA_USERNAME, ULV.AA_PASSWORD)
except (Exception, TimeoutException):
add_test_result(3, 5, "This test failed attempting to login with the user's credentials.")
add_test_result(4, 2, 'This test is blocked due to the failure of a preceding test in the suite.')
add_test_result(5, 2, 'This test is blocked due to the failure of a preceding test in the suite.')
add_test_result(6, 2, 'This test is blocked due to the failure of a preceding test in the suite.')
TestDealerSearchID.testSuiteResult = 1
self.fail(msg='AAv4 - Dealer: Search - Test failure.')
def test_dealer_search_id_exact(self):
if TestDealerSearchID.testSuiteResult == 1:
TestDealerSearchID.skipTest(self, 'This test is blocked due to the failure of a preceding test in the suite.')
else:
pass
REST OF CODE HERE AND REMAINING TWO TESTS WITH SAME IF/ELSE STATEMENT.
这是我能想到的最好的解决方案,而且它完全符合需要。如果有人知道更好的方法,请不要犹豫发表评论或 post 一个答案。
谢谢,
杰伊·赖特
我想我找到了解决这个问题的方法。它在 py.test 框架中被称为 incremental testing
:https://pytest.org/latest/example/simple.html#incremental-testing-test-steps
我在本地进行了测试 - 效果非常好!
使用与 py.test 文档中相同的 conftest.py
。
略有不同的测试文件:
# content of test_incremental.py
import pytest
@pytest.mark.incremental
class TestUserHandling:
def test_login(self):
assert 1
def test_modification(self):
assert 0
def test_deletion(self):
assert 1
def test_foo(self):
assert 1
class TestAnotherStuff:
def test_normal(self):
assert 1
mac:[incremental_testing]: py.test -v test_incremental.py
============================================================================== test session starts ===============================================================================
platform darwin -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
cachedir: .cache
rootdir: /Users/dmitry.tokarev/Repos/playground/incremental_testing, inifile:
plugins: hidecaptured-0.1.2, instafail-0.3.0
collected 5 items
test_incremental.py::TestUserHandling::test_login PASSED
test_incremental.py::TestUserHandling::test_modification FAILED
test_incremental.py::TestUserHandling::test_deletion xfail
test_incremental.py::TestUserHandling::test_foo xfail
test_incremental.py::TestAnotherStuff::test_normal PASSED
==================================================================================== FAILURES ====================================================================================
_______________________________________________________________________ TestUserHandling.test_modification _______________________________________________________________________
self = <test_incremental.TestUserHandling instance at 0x103b7a5a8>
def test_modification(self):
> assert 0
E assert 0
test_incremental.py:10: AssertionError
================================================================= 1 failed, 2 passed, 2 xfailed in 0.02 seconds ==================================================================