我正在使用pytest来测试我的应用程序.pytest支持2种方法(我知道)如何编写测试:
test_feature.py - >类TestFeature - > def test_feature_sanity
test_feature.py - > def test_feature_sanity
是否需要在课堂上分组测试的方法?是否允许backport unittest内置模块?你会说哪种方法更好,为什么?
提前致谢!
Jas*_*sha 21
这个答案为 pytest 中的 TestClass 提供了两个引人注目的用例:
pytest 参数化装饰器@pytest.mark.parametrize可用于使输入可用于类中的多个方法。在下面的代码中,输入param1和param2可用于每个方法TestGroup.test_one和TestGroup.test_two。
"""test_class_parametrization.py"""
import pytest
@pytest.mark.parametrize(
"param1,param2",
[
("a", "b"),
("c", "d"),
],
)
class TestGroup:
"""A class with common parameters, `param1` and `param2`."""
@pytest.fixture
def fixt(self):
"""This fixture will only be available within the scope of TestGroup"""
return 123
def test_one(self, param1, param2, fixt):
print("\ntest_one", param1, param2, fixt)
def test_two(self, param1, param2):
print("\ntest_two", param1, param2)
Run Code Online (Sandbox Code Playgroud)
$ pytest -s test_class_parametrization.py
================================================================== test session starts ==================================================================
platform linux -- Python 3.8.6, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /home/jbss
plugins: pylint-0.18.0
collected 4 items
test_class_parametrization.py
test_one a b 123
.
test_one c d 123
.
test_two a b
.
test_two c d
.
=================================================================== 4 passed in 0.01s ===================================================================
Run Code Online (Sandbox Code Playgroud)
我将使用从另一个答案中获取的修改后的代码版本来演示从TestClassto继承类属性/方法的有用性TestSubclass:
$ pytest -s test_class_parametrization.py
================================================================== test session starts ==================================================================
platform linux -- Python 3.8.6, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /home/jbss
plugins: pylint-0.18.0
collected 4 items
test_class_parametrization.py
test_one a b 123
.
test_one c d 123
.
test_two a b
.
test_two c d
.
=================================================================== 4 passed in 0.01s ===================================================================
Run Code Online (Sandbox Code Playgroud)
pytest在此文件上运行会导致运行四个测试:
$ pytest -v test_example.py
=========== test session starts ===========
platform linux -- Python 3.8.2, pytest-5.4.2, py-1.8.1
collected 4 items
test_example.py::TestClass::test_var_positive PASSED
test_example.py::TestSubclass::test_var_positive PASSED
test_example.py::TestSubclass::test_var_even PASSED
test_example.py::TestSubclass::test_data PASSED
Run Code Online (Sandbox Code Playgroud)
在子类中,继承的test_var_positive方法使用更新的值运行self.VAR == 8,新定义的test_data方法针对继承的属性运行self.DATA == 4。这种方法和属性继承提供了一种灵活的方式来重用或修改不同测试用例组之间的共享功能。
小智 17
关于将测试组织到模块和类中没有严格的规则.这是个人喜好的问题.最初我尝试将测试组织成课程,经过一段时间后,我意识到我没有用于其他级别的组织.现在我只是将测试功能收集到模块(文件)中.
我可以看到一个有效的用例,当某些测试可以逻辑地组织到同一个文件中,但仍然有更多的组织级别(例如,使用类范围的夹具).但这也可以分成多个模块来完成.
min*_*ief 13
通常在单元测试中,我们的测试对象是单个函数。也就是说,单个函数会产生多个测试。在阅读测试代码时,将单个单元的测试以某种方式组合在一起很有用(这也允许我们为特定功能运行所有测试),因此这给我们留下了两个选择:
在第一种方法中,我们仍然有兴趣utils.py以某种方式对与源模块(例如)相关的所有测试进行分组。现在,由于我们已经在使用模块对函数的测试进行分组,这意味着我们应该使用包来对源模块的测试进行分组。
结果是一个源函数映射到一个测试模块,一个源模块映射到一个测试包。
在第二种方法中,我们将一个源函数映射到一个测试类(例如my_function()-> TestMyFunction),一个源模块映射到一个测试模块(例如utils.py-> test_utils.py)。
这可能取决于具体情况,但第二种方法,即对您正在测试的每个函数进行一类测试,对我来说似乎更清楚。此外,如果我们正在测试源类/方法,那么我们可以简单地使用测试类的继承层次结构,并且仍然保留一个源模块 -> 一个测试模块映射。
最后,与仅包含多个函数测试的平面文件相比,这两种方法的另一个好处是,类/模块已经确定正在测试哪个函数,您可以为实际测试使用更好的名称,例如test_does_xandtest_handles_y而不是test_my_function_does_xand test_my_function_handles_y。
| 归档时间: |
|
| 查看次数: |
3614 次 |
| 最近记录: |