除了在 py.test class 中为每个变量创建一个夹具之外,还有其他选择吗?
Is there any alternative to creating one fixture per variable in a py.test class?
我想在我的所有 py.test class 方法中使用一些通用数据,并且仅在那个 class 中使用,例如
n_files = 1000
n_classes = 10
n_file_per_class = int(n_files / n_classes)
我发现我可以使用固定装置,例如:
class TestDatasplit:
@pytest.fixture()
def n_files(self):
return 1000
@pytest.fixture()
def n_classes(self):
return 10
@pytest.fixture()
def n_files_per_class(self, n_files, n_classes):
return int(n_files / n_classes)
def test_datasplit_1(self, n_files):
assert n_files == 1000
def test_datasplit(self, n_files_per_class):
assert n_files_per_class == 100
但在这里我需要为我的所有变量创建一个夹具,但这看起来很冗长(我有 3 个以上的变量)...
在 py.test class 中创建一堆共享变量的最佳方法是什么?
您的测试似乎没有改变这些值,因此您可以使用模块级或 class 级常量。 Pytest fixtures 可以为每个测试提供一个单独的值副本,这样当一个或多个测试改变值时,测试不会开始相互依赖(或无意中使彼此失败)。
我同意@das-g 所说的,但是如果你想使用固定装置,那么你可以有一个固定装置,其中 returns 一个基于自定义 class 的对象,或者例如namedtuple.
我想在我的所有 py.test class 方法中使用一些通用数据,并且仅在那个 class 中使用,例如
n_files = 1000
n_classes = 10
n_file_per_class = int(n_files / n_classes)
我发现我可以使用固定装置,例如:
class TestDatasplit:
@pytest.fixture()
def n_files(self):
return 1000
@pytest.fixture()
def n_classes(self):
return 10
@pytest.fixture()
def n_files_per_class(self, n_files, n_classes):
return int(n_files / n_classes)
def test_datasplit_1(self, n_files):
assert n_files == 1000
def test_datasplit(self, n_files_per_class):
assert n_files_per_class == 100
但在这里我需要为我的所有变量创建一个夹具,但这看起来很冗长(我有 3 个以上的变量)...
在 py.test class 中创建一堆共享变量的最佳方法是什么?
您的测试似乎没有改变这些值,因此您可以使用模块级或 class 级常量。 Pytest fixtures 可以为每个测试提供一个单独的值副本,这样当一个或多个测试改变值时,测试不会开始相互依赖(或无意中使彼此失败)。
我同意@das-g 所说的,但是如果你想使用固定装置,那么你可以有一个固定装置,其中 returns 一个基于自定义 class 的对象,或者例如namedtuple.