目录:
unittest.TestCase支持
pytest支持unittest开箱即用的基于Python 的测试。它旨在利用现有unittest的测试套件将pytest用作测试运行器,并允许逐步调整测试套件以充分利用pytest的功能。
要使用运行现有unittest样式的测试套件pytest,请键入:
pytest tests
pytest将自动收集unittest.TestCase子类及其test方法test_*.py或*_test.py文件。
几乎所有unittest功能都受支持:
@unittest.skip 风格装饰;
setUp/tearDown;
setUpClass/tearDownClass;
setUpModule/tearDownModule;
到目前为止,pytest不支持以下功能:
开箱即用的好处
通过使用pytest运行测试套件,你可以使用多种功能,在大多数情况下无需修改现有代码:
unittest.TestCase子类中的pytest特性
以下pytest功能适用于unittest.TestCase子类:
下面pytest功能不工作,也许永远也因不同的设计理念:
固定装置(autouse固定装置除外,见下文);
第三方插件可能运行也可能不运行,具体取决于插件和测试套件。
unittest.TestCase使用标记将pytest灯具混合到子类中
运行unittest pytest允许你使用其 夹具机制进行unittest.TestCase样式测试。假设你至少浏览了pytest fixture功能,让我们跳转到一个集成pytest db_class fixture,设置类缓存数据库对象,然后从unittest样式测试中引用它的示例:
# content of conftest.py
# we define a fixture function below and it will be "used" by
# referencing its name from tests
import pytest
@pytest.fixture(scope="class")
def db_class(request):
class DummyDB(object):
pass
# set a class attribute on the invoking test context
request.cls.db = DummyDB()
这定义了一个fixture函数db_class- 如果使用的话 - 为每个测试类调用一次,并将class-level db属性设置为一个DummyDB实例。fixture函数通过接收一个特殊request对象来实现这一点,该对象允许访问请求测试上下文,例如cls属性,表示使用该fixture的类。该架构将夹具写入与实际测试代码分离,并允许通过最小参考(夹具名称)重新使用夹具。那么让unittest.TestCase我们使用fixture定义编写一个实际的类:
# content of test_unittest_db.py
import unittest
import pytest
@pytest.mark.usefixtures("db_class")
class MyTest(unittest.TestCase):
def test_method1(self):
assert hasattr(self, "db")
assert 0, self.db # fail for demo purposes
def test_method2(self):
assert 0, self.db # fail for demo purposes
在@pytest.mark.usefixtures("db_class")类的装饰可确保pytest固定函数db_class被调用每一次班。由于故意失败的断言语句,我们可以看看self.db回溯中的值:
$ pytest test_unittest_db.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 2 items
test_unittest_db.py FF [100%]
================================= FAILURES =================================
___________________________ MyTest.test_method1 ____________________________
self =
def test_method1(self):
assert hasattr(self, "db")
> assert 0, self.db # fail for demo purposes
E AssertionError: .DummyDB object at 0xdeadbeef>
E assert 0
test_unittest_db.py:9: AssertionError
___________________________ MyTest.test_method2 ____________________________
self =
def test_method2(self):
> assert 0, self.db # fail for demo purposes
E AssertionError: .DummyDB object at 0xdeadbeef>
E assert 0
test_unittest_db.py:12: AssertionError
========================= 2 failed in 0.12 seconds =========================
这个默认的pytest回溯显示两个测试方法共享同一个self.db实例,这是我们在编写上面的类范围的fixture函数时的意图。
使用autouse灯具和访问其他灯具
虽然通常更好地明确声明对给定测试需要使用的灯具,但有时你可能想要在给定的上下文中自动使用灯具。毕竟,传统的unittest-setup风格要求使用这种隐含的夹具编写,而且很有可能,你习惯它或者喜欢它。
你可以使用标记夹具功能@pytest.fixture(autouse=True) 并在要使用它的上下文中定义夹具功能。让我们看一个initdir夹具,它使一个TestCase类的所有测试方法都 在一个预先初始化的临时目录中执行samplefile.ini。我们的initdirfixture本身使用pytest builtin tmpdir fixture来委托创建一个per-test临时目录:
# content of test_unittest_cleandir.py
import pytest
import unittest
class MyTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def initdir(self, tmpdir):
tmpdir.chdir() # change to pytest-provided temporary directory
tmpdir.join("samplefile.ini").write("# testdata")
def test_method(self):
with open("samplefile.ini") as f:
s = f.read()
assert "testdata" in s
由于该autouse标志,initdirfixture函数将用于定义它的类的所有方法。这是@pytest.mark.usefixtures("initdir")在类中使用标记的快捷方式,如上例所示。
运行此测试模块......:
$ pytest -q test_unittest_cleandir.py
. [100%]
1 passed in 0.12 seconds
...给我们一个通过测试,因为initdir夹具功能在之前执行test_method。
注意
unittest.TestCase 方法不能直接接收fixture参数作为实现可能会导致运行通用unittest.TestCase测试套件的能力。
以上usefixtures和autouse示例应该有助于将pytest灯具混合到unittest套件中。
你也可以逐步从子类化转移unittest.TestCase到普通断言 ,然后开始逐步从完整的pytest功能集中受益。
注意
从unittest.TestCase子类运行测试--pdb将禁用针对发生异常的情况的tearDown和cleanup方法。这允许对所有在其tearDown机器中具有重要逻辑的应用程序进行适当的事后调试。但是,支持此功能会产生以下副作用:如果人们覆盖unittest.TestCase __call__或者run需要以debug相同的方式覆盖(对于标准unittest也是如此)。
注意
由于两个框架之间的架构差异,在unittest测试call阶段而不是在pytest标准setup和teardown阶段中执行基于测试的设置和拆卸。在某些情况下,这一点非常重要,特别是在推理错误时。例如,如果unittest基于a 的套件在设置期间出现错误,pytest则在其setup阶段期间将报告没有错误, 并且将在此期间引发错误call。