简体   繁体   English

无法在python单元测试代码中使用tempfile库生成的目录

[英]Not able to use the directories generated by tempfile library in python Unit testing Code

I am unit testing my Python code, I am using tempfile library to create a temp directory called temp_dir and save the generated output file in my temp_dir, then I want to compare this output with the already existing correct output file. 我是单元测试我的Python代码,我使用tempfile库创建一个名为temp_dir的临时目录,并将生成的输出文件保存在我的temp_dir中,然后我想将此输出与已存在的正确输出文件进行比较。

I have my class, in its setup method, I am creating temp_dir and I am removing it in tear down method as follows: 我有我的类,在它的设置方法中,我正在创建temp_dir,我将在拆卸方法中删除它,如下所示:

class PrimaryTest(unittest.TestCase):

  def setUp(self):
    self.temp_dir = tempfile.mkdtemp()

  def tearDown(self):
    shutil.rmtree(self.temp_dir)

Now, this temp directory I want to use in my test function to save the file generated by the functions in my Binary, the one I am testing.. 现在,我想在我的测试函数中使用这个临时目录来保存由我的Binary中的函数生成的文件,我正在测试它。

Here is how I am using the above code: 以下是我使用上述代码的方法:

  def CatalogCmp(self, product_name, actual_file, expected_file):
    doom.getCatalog(self.temp_dir, product_name)
    actual = json.load(open(actual_file))
    expected = json.load(open(expected_file))
    self.assertEqual(actual, expected)

  def testCatalogImage(self):
    expected_path = os.path.join(‘path/testdata/doom’, ’product1.json')
    actual_path = os.path.join(self.temp_dir, ’product1.json')
    self.CatalogCmp(‘product1’, actual_path, expected_path)

I am getting the following error: 我收到以下错误:

IOError: [Errno 2] No such file or directory: '/tmp/tmpGmv_ZR/product1.json'

Following line is generating this error: 以下行生成此错误:

actual = json.load(open(actual_file))

I got some hints, which helped me to resolve the problem : 我得到了一些提示,这有助于我解决问题:

  1. Actually, I am creating the temp directory twice by calling self.temp_dir in the function CatalogCmp and before the call in the test function itself, during the actual_path variable creation, this creates two different directories for both the calls, although this might not be the direct cause of the given error, but it is wrong. 实际上,我通过在函数CatalogCmp中调用self.temp_dir并在test函数本身调用之前创建temp目录两次,在actual_path变量创建期间,这为两个调用创建了两个不同的目录,尽管这可能不是直接导致给定错误,但这是错误的。 So now I am creating the temp directory only once in my test function and saving the variable and passing the same to other places. 所以现在我只在test函数中创建一次temp目录并保存变量并将其传递给其他地方。

  2. One more thing which was wrong was that the function which I am testing actually accepts the list variable rather than just the product name. 还有一件事是错误的,我测试的函数实际上接受列表变量而不仅仅是产品名称。 So maybe the function being tested was not even generating any output. 因此,正在测试的功能可能甚至没有产生任何输出。 Sorry, I think someone here would have definitely helped, but I can't put all the code here. 对不起,我觉得这里有人肯定有帮助,但我不能把所有的代码都放在这里。 So when I tried calling my function like this: doom.GetCatalog(selftemp_dir, [product_name]). 所以当我尝试调用我的函数时:doom.GetCatalog(selftemp_dir,[product_name])。 It is working now. 它现在正在运作。 but I wonder why the error was not relevant to catch this error. 但我想知道为什么错误与捕获此错误无关。 How I would have checked if the function was even called properly or not. 我怎么会检查函数是否被正确调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM