写入tempfile Python(Unittest)

时间:2016-03-30 11:50:55

标签: python file-io

我正在尝试写入临时文件,然后读取文件并执行assertTrue以检查是否

import tempfile
import unittest
import MyFile

def testSomething(self):
    t = tempfile.TemporaryFile(prefix='TemporaryFile_', suffix='.txt', dir='C:\Test', delete=True)
    print t.name #This gives me a temporary file

    x = MyFile.main(["--path", t.name,
                       "--string", "XYZ = ",
                       "--number", "50"]) #The issue occurs here because "x" is None.
    t.write(x) #Here I want it to write to the temporary file
    with open(t.name, 'r') as f:
        content = f.read()

    self.assertTrue("XYZ = 20" in content)
    #Can probably use assertEquals as well.
    t.close()

我收到此错误TypeError: must be string or buffer, not None,因为“x”为无。

MyFile main接收路径名,字符串和数字。

0 个答案:

没有答案