sop*_*phe 5 python unit-testing pytest python-2.7
我正在使用py.test来构建功能测试框架,因此我需要能够指定要运行的确切测试.我理解动态测试集合的美妙之处,但我希望能够先运行我的测试环境运行状况检查,然后运行我的回归测试; 该分类并不排除将这些集合中的测试用于其他目的.
测试套件将与Jenkins构建项目绑定.我正在使用osx,python 2.7.3,py.test 2.3.4.
所以我有一个如下测试用例:
# sample_unittest.py
import unittest, pytest
class TestClass(unittest.TestCase):
def setUp(self):
self.testdata = ['apple', 'pear', 'berry']
def test_first(self):
assert 'apple' in self.testdata
def test_second(self):
assert 'pear' in self.testdata
def tearDown(self):
self.testdata = []
def suite():
suite = unittest.TestSuite()
suite.addTest(TestClass('test_first'))
return suite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的测试套件:
# suite_regression.py
import unittest, pytest
import functionaltests.sample_unittest as sample_unittest
# set up the imported tests
suite_sample_unittest = sample_unittest.suite()
# create this test suite
suite = unittest.TestSuite()
suite.addTest(suite_sample_unittest)
# run the suite
unittest.TextTestRunner(verbosity=2).run(suite)
Run Code Online (Sandbox Code Playgroud)
如果我从命令行对套件运行以下命令,test_first运行(但我没有获得py.test将提供的其他信息):
python functionaltests/suite_regression.py -v
如果我针对套件运行以下操作,则会收集0个测试:
py.test functionaltests/suite_regression.py
如果我针对测试用例运行以下命令,则运行test_first和test_second:
py.test functionaltests/sample_unittest.py -v
我没有看到如何使用关键字进行py.test有助于将测试组织到套件中.将测试用例放入文件夹结构并使用文件夹选项运行py.test将不允许我按功能区域组织测试.
所以我的问题:
编辑:所以我尝试了py.test标记,它允许我用任意标签标记测试函数和测试方法,然后在运行时过滤该标签.
# conftest.py
import pytest
# set up custom markers
regression = pytest.mark.NAME
health = pytest.mark.NAME
Run Code Online (Sandbox Code Playgroud)
我更新的测试用例:
# sample_unittest.py
import unittest, pytest
class TestClass(unittest.TestCase):
def setUp(self):
self.testdata = ['apple', 'pear', 'berry']
@pytest.mark.healthcheck
@pytest.mark.regression
def test_first(self):
assert 'apple' in self.testdata
@pytest.mark.regression
def test_second(self):
assert 'pear' in self.testdata
def tearDown(self):
self.testdata = []
def suite():
suite = unittest.TestSuite()
suite.addTest(TestClass('test_first'))
return suite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
Run Code Online (Sandbox Code Playgroud)
因此,运行以下命令将收集并运行test_first:
py.test functionaltests/sample_unittest.py -v -m healthcheck
这会收集并运行test_first和test_second:
py.test functionaltests/sample_unittest.py -v -m regression
回到我的问题:标记是部分解决方案,但我仍然没有办法控制收集的标记测试的执行.
在这种情况下不需要使用标记:@pytest.mark.incremental在 py.test 测试类上设置将强制执行顺序为声明顺序:
# sequential.py
import pytest
@pytest.mark.incremental
class TestSequential:
def test_first(self):
print('first')
def test_second(self):
print('second')
def test_third(self):
print('third')
Run Code Online (Sandbox Code Playgroud)
现在运行它
pytest -s -v sequential.py
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
=========== test session starts ===========
collected 3 items
sequential.py::TestSequential::test_first first
PASSED
sequential.py::TestSequential::test_second second
PASSED
sequential.py::TestSequential::test_third third
PASSED
=========== 3 passed in 0.01 seconds ===========
Run Code Online (Sandbox Code Playgroud)
目前没有直接的方法来控制测试执行的顺序。FWIW,有一个插件钩子pytest_collection_modifyitems可以用来实现一些东西。请参阅https://github.com/klrmn/pytest-random/blob/master/random_plugin.py了解使用它来实现随机化的插件。
| 归档时间: |
|
| 查看次数: |
8587 次 |
| 最近记录: |