python对函数进行单元测试_如何使用python unittest对函数进行单元测试

我有一个python函数,它将输出文件写入磁盘。

我想使用python unit test模块为它编写一个单元测试。

我应该如何断言文件的相等性?如果文件内容与预期的一+差异列表不同,我希望得到一个错误。在unix diff命令的输出中。

是否有任何官方/推荐的方法?

我更喜欢让输出函数显式地接受文件句柄(或类似文件的对象),而不是接受文件名并自己打开文件。这样,我可以在单元测试中将一个StringIO对象传递给输出函数,然后将该StringIO对象(在.seek(0)调用之后)的内容返回到该StringIO对象,并与预期的输出进行比较。

例如,我们将像这样转换代码

##File:lamb.py

import sys

def write_lamb(outfile_path):

with open(outfile_path, 'w') as outfile:

outfile.write("Mary had a little lamb.

")

if __name__ == '__main__':

write_lamb(sys.argv[1])

##File test_lamb.py

import unittest

import tempfile

import lamb

class LambTests(unittest.TestCase):

def test_lamb_output(self):

outfile_path = tempfile.mkstemp()[1]

try:

lamb.write_lamb(outfile_path)

contents = open(tempfile_path).read()

finally:

# NOTE: To retain the tempfile if the test fails, remove

# the try-finally clauses

os.remove(outfile_path)

self.assertEqual(result,"Mary had a little lamb.

")

这样编码

##File:lamb.py

import sys

def write_lamb(outfile):

outfile.write("Mary had a little lamb.

")

if __name__ == '__main__':

with open(sys.argv[1], 'w') as outfile:

write_lamb(outfile)

##File test_lamb.py

import unittest

from io import StringIO

import lamb

class LambTests(unittest.TestCase):

def test_lamb_output(self):

outfile = StringIO()

# NOTE: Alternatively, for Python 2.6+, you can use

# tempfile.SpooledTemporaryFile, e.g.,

#outfile = tempfile.SpooledTemporaryFile(10 ** 9)

lamb.write_lamb(outfile)

outfile.seek(0)

content = outfile.read()

self.assertEqual(content,"Mary had a little lamb.

")

这种方法还有一个额外的好处,那就是,如果您决定不想写入文件,而是使用其他缓冲区,那么您的输出函数就更加灵活,因为它将接受所有类似文件的对象。

注意,使用StringIO假设测试输出的内容可以装入主内存。对于非常大的输出,可以使用临时文件方法(例如tempfile.spooledtemporaryfile)。

这比将文件写入磁盘要好。如果您正在运行大量的单元测试,IO到磁盘会导致各种各样的问题,特别是试图清理它们。我有写磁盘的测试,拆卸删除写的文件。测试一次只能运行一个,然后在全部运行时失败。至少在Win机器上使用Visual Studio和pytools。此外,速度。

虽然这是一个测试单独函数的好解决方案,但是在测试程序提供的实际接口(例如cli工具)时,仍然很麻烦。

你可以用getvalue()代替seek()和read()。

我得到了错误:类型错误:需要unicode参数,得到了'str'

我来这里是因为我正试图编写单元测试来遍历和逐个文件读取分区的拼花数据集。这需要分析文件路径以获取键/值对,以便将分区的适当值分配给(最终)结果熊猫数据帧。写入缓冲区虽然很好,但无法解析分区值。

@pmende听起来您正在使用一个需要与实际文件系统交互的API。单元测试并不总是适当的测试级别。不在单元测试级别测试代码的所有部分是可以的;在适当的情况下,也应该使用集成或系统测试。但是,尝试包含这些部分,并尽可能在边界之间传递简单的值。看到youtube.com/watch了吗?V= EyAlAl8ELNZK

最后,我使用python的tempfile模块随机生成临时分区数据集。但你提出了一些好的想法。感谢您的输入!

最简单的方法是编写输出文件,然后读取其内容,读取黄金(预期)文件的内容,并将其与简单字符串相等进行比较。如果它们相同,请删除输出文件。如果他们不同,提出一个主张。

这样,当测试完成时,每一个失败的测试都将用一个输出文件表示,并且您可以使用第三方工具将它们与黄金文件进行比较(Beyond Compare非常适合这样做)。

如果您真的想提供自己的diff输出,请记住python stdlib有difflib模块。python 3.1中新的unittest支持包括一个assertMultiLineEqual方法,该方法使用它来显示差异,类似于:

def assertMultiLineEqual(self, first, second, msg=None):

"""Assert that two multi-line strings are equal.

If they aren't, show a nice diff.

"""

self.assertTrue(isinstance(first, str),

'First argument is not a string')

self.assertTrue(isinstance(second, str),

'Second argument is not a string')

if first != second:

message = ''.join(difflib.ndiff(first.splitlines(True),

second.splitlines(True)))

if msg:

message +=" :" + msg

self.fail("Multi-line strings are unequal:

" + message)

import filecmp

然后

self.assertTrue(filecmp.cmp(path1, path2))

您可以将内容生成与文件处理分开。这样,您就可以测试内容是否正确,而不必乱弄临时文件,然后再清理它们。

如果编写一个生成每行内容的生成器方法,那么您可以有一个文件处理方法,它打开一个文件并按行的顺序调用file.writelines()。这两个方法甚至可以在同一个类上:测试代码将调用生成器,生产代码将调用文件处理程序。

下面是一个例子,展示了所有三种测试方法。通常,您只需要选择一个方法,这取决于要测试的类中可用的方法。

import os

from io import StringIO

from unittest.case import TestCase

class Foo(object):

def save_content(self, filename):

with open(filename, 'w') as f:

self.write_content(f)

def write_content(self, f):

f.writelines(self.generate_content())

def generate_content(self):

for i in range(3):

yield u"line {}

".format(i)

class FooTest(TestCase):

def test_generate(self):

expected_lines = ['line 0

', 'line 1

', 'line 2

']

foo = Foo()

lines = list(foo.generate_content())

self.assertEqual(expected_lines, lines)

def test_write(self):

expected_text = u"""\

line 0

line 1

line 2

"""

f = StringIO()

foo = Foo()

foo.write_content(f)

self.assertEqual(expected_text, f.getvalue())

def test_save(self):

expected_text = u"""\

line 0

line 1

line 2

"""

foo = Foo()

filename = 'foo_test.txt'

try:

foo.save_content(filename)

with open(filename, 'rU') as f:

text = f.read()

finally:

os.remove(filename)

self.assertEqual(expected_text, text)

你能提供这个例子代码吗?听起来很有趣。

我为这三种方法添加了一个例子,@buhtz。

我总是尽量避免将文件写入磁盘,即使它是专门用于测试的临时文件夹:不实际接触磁盘会使测试更快,尤其是在代码中与文件进行大量交互时。

假设您在一个名为main.py的文件中有一个"惊人"的软件:

"""

main.py

"""

def write_to_file(text):

with open("output.txt","w") as h:

h.write(text)

if __name__ =="__main__":

write_to_file("Every great dream begins with a dreamer.")

要测试write_to_file方法,您可以在名为test_main.py的同一文件夹中编写类似的文件:

"""

test_main.py

"""

from unittest.mock import patch, mock_open

import main

def test_do_stuff_with_file():

open_mock = mock_open()

with patch("main.open", open_mock, create=True):

main.write_to_file("test-data")

open_mock.assert_called_with("output.txt","w")

open_mock.return_value.write.assert_called_once_with("test-data")

根据我的建议,我做了以下工作。

class MyTestCase(unittest.TestCase):

def assertFilesEqual(self, first, second, msg=None):

first_f = open(first)

first_str = first_f.read()

second_f = open(second)

second_str = second_f.read()

first_f.close()

second_f.close()

if first_str != second_str:

first_lines = first_str.splitlines(True)

second_lines = second_str.splitlines(True)

delta = difflib.unified_diff(first_lines, second_lines, fromfile=first, tofile=second)

message = ''.join(delta)

if msg:

message +=" :" + msg

self.fail("Multi-line strings are unequal:

" + message)

我创建了一个子类mytestcase,因为我有很多函数需要读/写文件,所以我真的需要有可重用的assert方法。现在在我的测试中,我将把my testcase子类化,而不是unittest.testcase。

你觉得怎么样?

参见stackoverflow.com/questions/4617034/…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值