带有命令行参数的单元测试 [英] Unittest with command-line arguments

查看:132
本文介绍了带有命令行参数的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我从另一篇SO帖子中了解到的,要对通过argparse接受命令行参数的脚本进行单元测试,我应该做类似下面的代码,将sys.argv [0]设为arg.

From what I understand from another SO post, to unittest a script that takes command line arguments through argparse, I should do something like the code below, giving sys.argv[0] as arg.

import unittest
import match_loc

class test_method_main(unittest.TestCase):

    loc = match_loc.main()
    self.assertEqual(loc, [4])


if __name__ == '__main__':
    sys.argv[1] = 'aaaac'
    sys.argv[2] = 'ac'
    unittest.main(sys.argv[0])

这将返回错误:

usage: test_match_loc.py [-h] text patterns [patterns ...]
test_match_loc.py: error: the following arguments are required: text, patterns

我想更深入地了解这里发生的事情.我了解

I would like to understand what is going on here deeper. I understand

if __name__ == '__main__': main()

if __name__ == '__main__': main()

表示,如果这是由"main"(最高级别的默认解释器)执行的,则仅自动运行"main"方法.我假设

says that if this is being executed by the 'main', highest level, default interpreter, to just automatically run the 'main' method. I'm assuming

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

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

恰好是您所说的运行unittest脚本的方式.

just happens to be the way you say this for running unittest scripts.

我知道运行任何脚本时,它都会自动具有一个argv对象,这是一个收集命令行中所有项目的向量.

I understand when any script is run, it automatically has an argv object, a vector collecting all the items on the command line.

但是我不明白unittest.main(sys.arg[0])会做什么. 'unittest.main'与参数有什么关系?我如何预先设置sys.argv的值-每次运行脚本时它不会自动重置吗?此外,如果在任何脚本之外,此对象"sys.argv"在哪里存在?最后,实现命令行参数测试的正确方法是什么?

But I do not understand what unittest.main(sys.arg[0]) would do. What does 'unittest.main' do with arguments? How can I pre-set the values of sys.argv - doesn't it automatically reset every time you run a script? Furthermore, where does this object 'sys.argv' exist, if outside of any script? Finally, what is the correct way to implement tests of command-line arguments?

如果我的问题含糊不清且被误导了,我感到抱歉.我想了解这里的所有相关组件,这样我才能真正了解自己在做什么.

I am sorry if my questions are vague and misguided. I would like to understand all the components relevant here so I can actually understand what I am doing.

非常感谢您.

推荐答案

仅通过处理一对简单文件,我发现在调用方模块主体中修改sys.argv会影响sys.argv导入的模块看到:

Just by playing around with a pair of simple files, I find that modifying sys.argv in the body of the caller module affects the sys.argv that the imported module sees:

import sys
sys.argv[1] = 'aaaac'
sys.argv[2] = 'ac'
class test_method_main(unittest.TestCase):
   ...

但是,像您一样在main块中修改sys.argv,并不会显示在导入的代码中.我们可以深入研究文档(和代码)以确切地了解原因,但是我认为仅确定有效的方法就足够了.

But modifying sys.argv in the main block as you do, does not show up in the imported one. We could dig into the documentation (and code) to see exactly why, but I think it's enough to just identify what works.

这是我从上一个导入模块的问题中重新构建的内容-带有一些诊断打印文件

Here's what I reconstructed from your previous question of the imported module - with a few diagnostic prints

import argparse
import sys
def main():
    print(sys.argv)
    parser = argparse.ArgumentParser(
            description='Takes a series of patterns as fasta files'
            ' or strings and a text as fasta file or string and'
            ' returns the match locations by constructing a trie.')
    parser.add_argument('text')
    parser.add_argument('patterns', nargs='+')
    args = parser.parse_args()
    print(args)
    return 1

您还可以使用自己的字符串列表来测试解析器,从而认识到parse_args如果sys.argv[1:]的参数缺失或不使用,则使用sys.argv[1:]:

You could also test a parser with your own list of strings, recognising that parse_args uses sys.argv[1:] if its argument is missing or None:

def main(argv=None):
    print(argv)
    ...
    args = parser.parse_args(argv)
    print(args)
    return 1

loc = match_loc.main(['abc','ab'])  # and in the caller

即使我能够构建一个有效的测试用例,您实际上也应该已经提供了足够的信息,而我不需要猜测或挖掘.

Even though I was able to construct a working test case, you really should have given enough information that I didn't need to guess or dig around.

这篇关于带有命令行参数的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆