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

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

问题描述

根据我从另一篇 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

如果 __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

如果 __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:] 如果其参数丢失或无:

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天全站免登陆