pytest 跳过其他继承 类
pytest skip on other Inheritance classes
我对 pytest 和 class 继承有疑问。
当pytest加载一个标记为skip的继承class时,所有其他兄弟也标记为skip。
这仅在 运行 --collect-only 执行期间发生,一切看起来都很好,只有相关的 class return 作为跳过。
我的项目class结构是:
basePreperationClass(object)
\
baseTestsClass(basePreperationClass)
| \
| spesisic_1_PreperationClass(baseTestsClass)
| |\
| | TestClass1(spesisic_1_PreperationClass)
| |\
| | @pytest.mark.skip(reasun="Test Class not Implemented")
| | TestClass2(spesisic_1_PreperationClass)
| |\
| | TestClass3(spesisic_1_PreperationClass)
| \
| spesisic_2_PreperationClass(baseTestsClass)
.
.
当我用跳过标记 TestClass2 时,所有下一个使用 spesisic_1_PreperationClass 的测试 class 也被跳过
这是怎么回事,为什么会这样,还有其他方法吗?将继承 class 标记为跳过而不影响其他测试 classes
作为解决方法,目前,我只是将 TestClass2.__test__
标记为 False,因此根本不会收集它。
运行 它未实现的原因是通过获取未执行的测试
在报告结果中获得实际覆盖率
重现的更多细节
python 2.7.16(我的代码还没有升级到 3.9)
pytest 版本 3.5.1
pytest-sugar-0.9.2
测试文件如下所示:
import pytest
class BasePreper_1(object):
__test__ = False
class BaseTests_1(BasePreper_1): # dont run this tests
__test__ = False
def test_1(self):
pass
def test_2(self):
pass
class ExtendPreper_1(BaseTests_1):
__test__ = False
class TestClass1(ExtendPreper_1): # dont run this tests
__test__ = False
def test_3(self):
pass
@pytest.mark.skip(reason="Event class not implemented yet")
class TestClass2(ExtendPreper_1): # skip this tests
__test__ = True
def test_3(self):
pass
class TestClass3(ExtendPreper_1): # run this tests - this is the issue where inheritanced tests also skiped
__test__ = True
def test_3(self):
pass
class BaseTests_2(BaseTests_1): # don't run tests - if you like to change __test__ to True and you will see that its also skpiped while expected to execute
__test__ = False
class TestClass4(TestClass2): # this tests expected to skiped
def test_4(self):
pass
运行 日志看起来像这样
Test session starts (platform: darwin, Python 2.7.16, pytest 3.5.1, pytest-sugar 0.9.2)
cachedir: .pytest_cache
rootdir: //tmp/pytest_tests, inifile:
plugins: sugar-0.9.2
test_preper_1.py::BaseTests_1.test_1 ✓ 8% ▉
test_preper_1.py::BaseTests_1.test_2 ✓ 17% █▋
test_preper_1.py::TestClass2.test_1 s 25% ██▌
test_preper_1.py::TestClass2.test_2 s 33% ███▍
test_preper_1.py::TestClass2.test_3 s 42% ████▎
test_preper_1.py::TestClass3.test_1 s 50% █████
test_preper_1.py::TestClass3.test_2 s 58% █████▉
test_preper_1.py::TestClass3.test_3 ✓ 67% ██████▋
test_preper_1.py::TestClass4.test_1 s 75% ███████▌
test_preper_1.py::TestClass4.test_2 s 83% ████████▍
test_preper_1.py::TestClass4.test_3 s 92% █████████▎
test_preper_1.py::TestClass4.test_4 s 100% ██████████
Results (0.03s):
3 passed
9 skipped
####### 当我 运行 使用 python3.9 和 pytest6.2.1 时,问题没有重现 #######
/tmp/pytest_tests python3 -m pytest -vvvv 340ms Tue Dec 29 15:18:28 2020
========================================================================================== test session starts ==========================================================================================
platform darwin -- Python 3.9.0, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- /usr/local/opt/python@3.9/bin/python3.9
cachedir: .pytest_cache
rootdir: /tmp/pytest_tests
collected 10 items
test_preper_1.py::TestClass2::test_1 SKIPPED (Event class not implemented yet) [ 10%]
test_preper_1.py::TestClass2::test_2 SKIPPED (Event class not implemented yet) [ 20%]
test_preper_1.py::TestClass2::test_3 SKIPPED (Event class not implemented yet) [ 30%]
test_preper_1.py::TestClass3::test_1 PASSED [ 40%]
test_preper_1.py::TestClass3::test_2 PASSED [ 50%]
test_preper_1.py::TestClass3::test_3 PASSED [ 60%]
test_preper_1.py::TestClass4::test_1 SKIPPED (Event class not implemented yet) [ 70%]
test_preper_1.py::TestClass4::test_2 SKIPPED (Event class not implemented yet) [ 80%]
test_preper_1.py::TestClass4::test_3 SKIPPED (Event class not implemented yet) [ 90%]
test_preper_1.py::TestClass4::test_4 SKIPPED (Event class not implemented yet) [100%]
===================================================================================== 3 passed, 7 skipped in 0.02s ======================================================================================
已解决:
看起来像是 pytest 3.5.1 中的错误 - 将 pytest 升级到 4.6.11 时问题已解决
将 pytest 升级到 4.6.11 解决了这个问题
我对 pytest 和 class 继承有疑问。 当pytest加载一个标记为skip的继承class时,所有其他兄弟也标记为skip。 这仅在 运行 --collect-only 执行期间发生,一切看起来都很好,只有相关的 class return 作为跳过。
我的项目class结构是:
basePreperationClass(object)
\
baseTestsClass(basePreperationClass)
| \
| spesisic_1_PreperationClass(baseTestsClass)
| |\
| | TestClass1(spesisic_1_PreperationClass)
| |\
| | @pytest.mark.skip(reasun="Test Class not Implemented")
| | TestClass2(spesisic_1_PreperationClass)
| |\
| | TestClass3(spesisic_1_PreperationClass)
| \
| spesisic_2_PreperationClass(baseTestsClass)
.
.
当我用跳过标记 TestClass2 时,所有下一个使用 spesisic_1_PreperationClass 的测试 class 也被跳过
这是怎么回事,为什么会这样,还有其他方法吗?将继承 class 标记为跳过而不影响其他测试 classes
作为解决方法,目前,我只是将 TestClass2.__test__
标记为 False,因此根本不会收集它。
运行 它未实现的原因是通过获取未执行的测试
python 2.7.16(我的代码还没有升级到 3.9) pytest 版本 3.5.1 pytest-sugar-0.9.2
测试文件如下所示:
import pytest
class BasePreper_1(object):
__test__ = False
class BaseTests_1(BasePreper_1): # dont run this tests
__test__ = False
def test_1(self):
pass
def test_2(self):
pass
class ExtendPreper_1(BaseTests_1):
__test__ = False
class TestClass1(ExtendPreper_1): # dont run this tests
__test__ = False
def test_3(self):
pass
@pytest.mark.skip(reason="Event class not implemented yet")
class TestClass2(ExtendPreper_1): # skip this tests
__test__ = True
def test_3(self):
pass
class TestClass3(ExtendPreper_1): # run this tests - this is the issue where inheritanced tests also skiped
__test__ = True
def test_3(self):
pass
class BaseTests_2(BaseTests_1): # don't run tests - if you like to change __test__ to True and you will see that its also skpiped while expected to execute
__test__ = False
class TestClass4(TestClass2): # this tests expected to skiped
def test_4(self):
pass
运行 日志看起来像这样
Test session starts (platform: darwin, Python 2.7.16, pytest 3.5.1, pytest-sugar 0.9.2)
cachedir: .pytest_cache
rootdir: //tmp/pytest_tests, inifile:
plugins: sugar-0.9.2
test_preper_1.py::BaseTests_1.test_1 ✓ 8% ▉
test_preper_1.py::BaseTests_1.test_2 ✓ 17% █▋
test_preper_1.py::TestClass2.test_1 s 25% ██▌
test_preper_1.py::TestClass2.test_2 s 33% ███▍
test_preper_1.py::TestClass2.test_3 s 42% ████▎
test_preper_1.py::TestClass3.test_1 s 50% █████
test_preper_1.py::TestClass3.test_2 s 58% █████▉
test_preper_1.py::TestClass3.test_3 ✓ 67% ██████▋
test_preper_1.py::TestClass4.test_1 s 75% ███████▌
test_preper_1.py::TestClass4.test_2 s 83% ████████▍
test_preper_1.py::TestClass4.test_3 s 92% █████████▎
test_preper_1.py::TestClass4.test_4 s 100% ██████████
Results (0.03s):
3 passed
9 skipped
####### 当我 运行 使用 python3.9 和 pytest6.2.1 时,问题没有重现 #######
/tmp/pytest_tests python3 -m pytest -vvvv 340ms Tue Dec 29 15:18:28 2020
========================================================================================== test session starts ==========================================================================================
platform darwin -- Python 3.9.0, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- /usr/local/opt/python@3.9/bin/python3.9
cachedir: .pytest_cache
rootdir: /tmp/pytest_tests
collected 10 items
test_preper_1.py::TestClass2::test_1 SKIPPED (Event class not implemented yet) [ 10%]
test_preper_1.py::TestClass2::test_2 SKIPPED (Event class not implemented yet) [ 20%]
test_preper_1.py::TestClass2::test_3 SKIPPED (Event class not implemented yet) [ 30%]
test_preper_1.py::TestClass3::test_1 PASSED [ 40%]
test_preper_1.py::TestClass3::test_2 PASSED [ 50%]
test_preper_1.py::TestClass3::test_3 PASSED [ 60%]
test_preper_1.py::TestClass4::test_1 SKIPPED (Event class not implemented yet) [ 70%]
test_preper_1.py::TestClass4::test_2 SKIPPED (Event class not implemented yet) [ 80%]
test_preper_1.py::TestClass4::test_3 SKIPPED (Event class not implemented yet) [ 90%]
test_preper_1.py::TestClass4::test_4 SKIPPED (Event class not implemented yet) [100%]
===================================================================================== 3 passed, 7 skipped in 0.02s ======================================================================================
已解决: 看起来像是 pytest 3.5.1 中的错误 - 将 pytest 升级到 4.6.11 时问题已解决
将 pytest 升级到 4.6.11 解决了这个问题