Python pytest tmpdir是否仅与功能参数关联?

Python pytest tmpdir是否仅与功能参数关联?,python,pytest,Python,Pytest,有没有一种方法可以使用pytestfixture tmrdir编写类的测试方法?在文档中,它指定它可以与函数一起使用。 如果有一种方法可以为类中的测试方法传递tmpdir参数,请您分享一个例子好吗 我尝试了以下操作,但出现了如下错误: " test_method()只接受2个参数(给定1个参数)” 我的代码: import pytest class class_test(TestCase): def test_method(self,tmpdir): # code

有没有一种方法可以使用pytestfixture tmrdir编写类的测试方法?在文档中,它指定它可以与函数一起使用。

如果有一种方法可以为类中的测试方法传递tmpdir参数,请您分享一个例子好吗

我尝试了以下操作,但出现了如下错误: "

test_method()只接受2个参数(给定1个参数)”

我的代码:

import pytest

class class_test(TestCase):

    def test_method(self,tmpdir):
        # code

请帮忙

如文档中所述,您必须在initdir函数中添加tmpdir参数。 这样,initdirfixture函数将用于类的所有方法

例如:

import unittest
import pytest

   class Test_Temp(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_file content(self):
          with open('samplefile.ini', 'r') as f:
             assert f.read() == '# testdata'   //True


不能在
unittest
test类中将夹具作为测试参数传递,只能在测试函数中传递。查看通过autousing在
unittest
测试类中应用fixture的示例,它恰好是
tmpdir
用法。感谢您@hoefling的回复。我使用了来自的第一个片段的代码。即使在那之后,它似乎也不起作用。我还能错过什么?