如何为Python命令行T编写单元测试

2024-04-24 12:50:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在一个Python项目中努力获得更好的代码覆盖率,但是我还没有找到一个关于如何测试命令行工具的好例子。这是我的密码:

https://github.com/OpenDataAlex/etlTest/blob/dev/etltest/etlTest.py

我本想在帖子中包含代码,但我不想只发布一个片段。我正在构建一个运行项目其他组件的命令行工具。你知道吗

我编写的测试如下所示:

def test_in_file_custom_output_generation_output(self):
    file_param = "-f {0:s}".format(self.in_file)
    output_file = os.path.join(SettingsManager().get_file_location(),
                                   'etltest/samples/output/main/in_file_generation.txt')

    given_result = subprocess.check_output(args=['python', self.process, file_param, "-g"])

    with open(output_file, 'r') as f:
        expected_result = f.read()

    self.assertEqual(given_result, expected_result)

def test_parser_no_args(self):
    #Test if no args returns help options.
    subprocess.check_output(args=['python', self.process], stderr=subprocess.STDOUT)

我对为这样的项目编写测试还是个新手,希望能得到一些指导。我应该使用另一个测试工具Python的unittest工具吗?你知道吗


Tags: 工具项目代码命令行intestselfoutput