fro*_*hem 5 python command-line parsing unit-testing nose
在我的主要代码中,我有这个:
#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
Run Code Online (Sandbox Code Playgroud)
和我的测试文件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)
...
Run Code Online (Sandbox Code Playgroud)
当我试图运行nosetests测试函数的结果时afunc(...),我得到了这个错误:
machine:project user$ nosetests verify
usage: nosetests [-h] [-n DEG] [-k K] [-l L]
nosetests: error: unrecognized arguments: verify
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题呢?
好的,我刚刚通过添加几行if else条件解决了这个问题.
它似乎是我的测试文件(verify.py)无法管理解析器部分中的值赋值comp.py.所以,我只是添加下面的一些条件来分配的值deg,k,l在这种情况下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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1959 次 |
| 最近记录: |