[英]How can I organise my unit tests to run in a particular order in Python Nosetests
[英]Can not run nosetests when i use argparse in my python code
在我的主要代码中,我有这个:
#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
如何解决这个问题呢?
好的,我刚刚通过添加几行if else条件解决了这个问题。
似乎我的测试文件( verify.py
)无法管理comp.py
解析器部分的值赋值。 因此,我只是在下面添加一些条件来指定deg
, k
, l
的值,以防comp.py
不作为main函数运行。
#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
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
。 然后Nose会自动发现文件。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.