【问题标题】:Can not run nosetests when i use argparse in my python code当我在我的 python 代码中使用 argparse 时无法运行鼻子测试
【发布时间】:2014-06-24 22:16:01
【问题描述】:

在我的主要代码中,我有这个:

  #comp.py
  parser = ArgumentParser()
  parser.add_argument("-n", dest="deg", default=100,type=int, help="setup value of deg")
  parser.add_argument("-k", dest="k", default=25, type=float, help="setup value of k")
  parser.add_argument("-l", dest="l", default=0, type=int, help="setup value of l")
  args = parser.parse_args()

  def afunc(x):
      ...
      #do something with k, l, deg and the return the result
      ...
      return result

还有我的测试文件verify.py:

  #verify.py
  import unittest
  import comp
  class TestFuncs(unittest.TestCase):
      def test_afunc(self):
          self.assertEqual(afunc(0), 0)
          self.assertEqual(afunc(1), 0)
          self.assertEqual(afunc(1), 1)
          self.assertEqual(afunc(3.2), 1)
...

当我尝试运行nosetests 来测试函数afunc(...) 的结果时,我得到了这个错误:

machine:project user$ nosetests verify
usage: nosetests [-h] [-n DEG] [-k K] [-l L]
nosetests: error: unrecognized arguments: verify

如何解决这个问题?

【问题讨论】:

  • 你是怎么开始nose的?您要测试的文件的名称是什么,完整的内容是什么? (或内容尽可能短,但仍然显示问题)。你希望nose 做什么?
  • 看起来nosetests 可执行文件认为verify 是一个参数而不是一个文件。也许你可以给它你的verify.py文件的完整路径或者从更合适的位置运行nosetests?
  • @JanVlcinsky 和 ​​Yani 请检查我的答案,它对我有用。如果您有任何其他想法,请添加一些答案或放置一些 cmets,谢谢您的帮助。
  • @teces907 实际上,问题可能要简单得多-您对nosetests 的调用正在传递参数verify,它应该是verify.py
  • @terces907 无需为糟糕的体验道歉。您正在改进它,事实上,它是永无止境的迭代过程。

标签: python unit-testing parsing command-line nose


【解决方案1】:

好的,我刚刚通过添加几行 if else 条件解决了这个问题。

似乎是我的测试文件 (verify.py) 无法管理 comp.py 中解析器部分的值分配。所以,我只是在下面添加一些条件来分配degkl 的值,以防comp.py 不作为主函数运行。

#comp.py
if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("-n", dest="deg", default=100,type=int, help="setup value of deg")
    parser.add_argument("-k", dest="k", default=25, type=float, help="setup value of k")
    parser.add_argument("-l", dest="l", default=0, type=int, help="setup value of l")
    args = parser.parse_args()
else:
    deg=100
    k=25
    l=0

def afunc(x):
    ...
    #do something with k, l, deg and the return the result
    ...
    return result

【讨论】:

    【解决方案2】:

    更新comp.py

    • 仅从if __name__ == "__main__" 块运行实际代码。这允许安全导入模块并在作为脚本调用时运行代码
    • 单独的参数解析成main函数
    • 让经过测试的afunc 尽可能独立于其余部分,因此希望所有需要的输入都通过参数传递,而不是通过sys.argv 或其他全局状态。这使您的函数更易于测试

    代码来了:

    from argparse import ArgumentParser
    
    def main():
        parser = ArgumentParser()
        parser.add_argument("-n", dest="deg", default=100,type=int, help="setup value of deg")
        parser.add_argument("-k", dest="k", default=25, type=float, help="setup value of k")
        parser.add_argument("-l", dest="l", default=0, type=int, help="setup value of l")
        args = parser.parse_args()
    
    def afunc(x):
        return 2 * x
    
    if __name__ == "__main__":
        main()
    

    更新veryfi.py => test_it.py

    • 调用文件test_(something).py。然后鼻子会自动发现文件。
    • 定义明确的测试焦点,在这种情况下忽略参数解析并仅测试afunc
    • (推荐)拆分为更小的测试用例 - 您的测试运行报告会更好地告诉您真正的问题所在。
    • (鼻子功能)使用测试生成器,如test_with_gen所示。
    • 可能将测试移动到tests 子目录中。只是一种习惯。并且它允许将 nose 定位到更精确的测试。

    这里更新test_it.py:

    import unittest
    from comp import afunc
    
    class TestFuncs(unittest.TestCase):
      def test_afunc(self):
          self.assertEqual(afunc(0), 0)
          self.assertEqual(afunc(1), 2)
          self.assertEqual(afunc(2), 4)
          self.assertEqual(afunc(3.2), 6.4)
    
    def test_case_0():
        assert afunc(0) == 0
    
    def test_case_1():
        assert afunc(1) == 2
    
    def test_with_gen():
        plan = [(0, 0), (1, 2), (2, 4), (3.2, 6.4)]
        for arg, expected in plan:
            yield check_afunc, arg, expected
    
    def check_afunc(arg, expected):
        assert afunc(arg) == expected
    

    运行测试:

    $ nosetests -v
    test_afunc (test_it.TestFuncs) ... ok
    test_it.test_case_0 ... ok
    test_it.test_case_1 ... ok
    test_it.test_with_gen(0, 0) ... ok
    test_it.test_with_gen(1, 2) ... ok
    test_it.test_with_gen(2, 4) ... ok
    test_it.test_with_gen(3.2, 6.4) ... ok
    
    ----------------------------------------------------------------------
    Ran 7 tests in 0.005s
    
    OK
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 2010-10-28
      • 2020-10-12
      相关资源
      最近更新 更多