更正 argparse 的 class 结构,用于单元测试

Correct class structure for arg parse, for unittests

我的脚本:

class Pivot:
    def __init__(self):
        self.args = self.parse_args()

        self.path = os.getcwd()
        self.file = self.args.filename

        if self.args.delimiter:
            self.delimiter = self.args.delimiter
        else:
            self.delimiter = ";"


    def parse_args(self):
        parser = argparse.ArgumentParser(prog="TBD",
                                     description="TBD")

        parser.add_argument("-f", "--filename", help="blahblah")
        parser.add_argument("-d", "--delimiter", help="blahblah")
        args = parser.parse_args()

        return args

if __name__ == "__main__":
    p = Pivot()

我目前的test.py:

class TestPivot(unittest.TestCase):
    def setUp(self):
        self.pivot = Pivot()

    def tearDown(self):
        pass

    
    def test_parse_args(self):
        parsed = self.parser.parse_args(['-f', 'test'])
        self.assertEqual(parsed.something, 'test')


    def test_minimal(self):
        """To make sure at least a test is passed"""
        self.assertEqual("neo".upper(), "NEO")

然后当我 运行 python3 -m unittest tests/test_Pivot.py 时,我得到:

E
usage: Pivot table representation [-h] [-f FILENAME] [-d DELIMITER]
Pivot table representation: error: unrecognized arguments: tests/test_Pivot.py
E
======================================================================
ERROR: test_create_pivot (tests.test_Pivot.Pivot)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Project/tests/test_Pivot.py", line 21, in setUp
    self.pivot = Pivot()
  File "/Project/Pivot.py", line 25, in __init__
    self.args = self.parse_args()
  File "/Project/Pivot.py", line 42, in parse_args
    args = parser.parse_args()

我的 class 在处理 argparse 时是否应该以不同的方式重组,如果是,如何重组?目前我什至无法对 运行 进行简单测试,因为 class 是如何被实例化的。

感谢 (@MrBean Bremen) 的建议,我重构了我的代码,因此 parse_args 现在位于我的 class 之外:

def parse_args(args):
    parser = argparse.ArgumentParser(prog="blah",
                                     description="blah")

    parser.add_argument("-f", "--filename", help="blah")
    parser.add_argument("-d", "--delimiter", help="blah")

    return parser.parse_args(args)

class Pivot:
    def __init__(self, args):
        self.path = os.getcwd()
        self.file = args.filename

        if args.delimiter:
            self.delimiter = args.delimiter
        else:
            self.delimiter = ";"


if __name__ == "__main__":
    args = parse_args(sys.argv[1:])
    p = Pivot(args)

然后我的测试变成:

class TestPivot(unittest.TestCase):
    def setUp(self):
        args = parse_args(["-f", "test1", "-d", ","])
        self.pivot = Pivot(args)

    def test_init(self):
        self.assertEqual(self.pivot.file, "test1")
        self.assertEqual(self.pivot.delimiter, ",")

    def test_parse_args(self):
        parsed = parse_args(["-f", "test2"])
        self.assertEqual(parsed.filename, "test2")
        self.assertNotEqual(parsed.filename, "test3")