2

我在下面的文件中运行了单元测试,其中一个测试用例失败了,它不应该失败。我得到了一个意外的结果 - 断言错误,我打算在 TestFormatInitMethodArgs 中测试 if '"' == '"',但它测试了'"' == None- 它看起来像第二个测试用例中的测试检查不是它自己的 setUp() 的相等性:

#!/usr/bin/env python

import csv
import unittest

class Format:
    def __init__(self, file_path, header=False, flag='r', delimiter=',', quote_char=None):
        self.file_path = file_path
        self.header = header
        self.flag = flag
        self.delimiter = delimiter
        self.quote_char = None

class TestFormatInitMethodDefaults(unittest.TestCase):
    def setUp(self):
        self.file_path = 'C:/Privatus/eurusd.csv'
        self.header = False
        self.flag = 'r'
        self.delimiter = ','
        self.quote_char = None

    def test_attributes(self):
        f = Format('C:/Privatus/eurusd.csv')
        self.assertEqual(self.file_path, f.file_path)
        self.assertEqual(self.header, f.header)
        self.assertEqual(self.flag, f.flag)
        self.assertEqual(self.delimiter, f.delimiter)
        self.assertEqual(self.quote_char, f.quote_char)

class TestFormatInitMethodArgs(unittest.TestCase):

    def setUp(self):
        self.file_path = 'C:/Privatus/eurusd.csv'
        self.header = True
        self.flag = 'rb'
        self.delimiter = ';'
        self.quote_char = '"'

    def test_args(self):
        a = Format('C:/Privatus/eurusd.csv', header=True, flag='rb', delimiter=';', quote_char='"')
        self.assertEqual(self.file_path, a.file_path)
        self.assertEqual(self.header, a.header)
        self.assertEqual(self.flag, a.flag)
        self.assertEqual(self.delimiter, a.delimiter)
        self.assertEqual(self.quote_char, a.quote_char)

if __name__ == '__main__':
    unittest.main()

测试结果:

F.
======================================================================
FAIL: test_args (__main__.TestFormatInitMethodArgs)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Privatus\repos\working\data.py", line 45, in test_args
    self.assertEqual(self.quote_char, a.quote_char)
AssertionError: '"' != None

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=1)

我究竟做错了什么?

4

1 回答 1

3

您似乎没有quote_char在构造函数(__init__)中设置属性

尝试self.quote_char = quote_char代替self.quote_char = None

于 2013-03-05T09:33:10.253 回答