简体   繁体   English

单元测试使用python运行,但由于鼻子测试失败

[英]unit test runs with python but fails with nosetests

I have written a python file containing some unit tests (testfile.py). 我已经编写了一个包含一些单元测试的Python文件(testfile.py)。

When I run 'python testfile.py' the tests run fine and there are no errors. 当我运行“ python testfile.py”时,测试运行正常,没有错误。

But when I run 'nosetests testfile.py', I get a TypeError of the form TypeError: func_name() takes exactly 3 arguments (1 given). 但是当我运行'nosetests testfile.py'时,我得到TypeError形式的TypeError:func_name()恰好接受3个参数(给定1个)。

Can you please help me understand what might be going on and how can I modify the python file so that it can be run using both python and nosetests. 您能否帮助我了解可能发生的情况以及如何修改python文件,以便可以同时使用python和鼻子测试运行它。 Thanks in advance, Ambarish. 在此先感谢,Ambarish。

Yeah, you're getting hung on how discovery works differently on the different tools. 是的,您迷上了发现如何在不同的工具上以不同的方式工作。 It trips me up all the time. 它使我无时无刻不在跳下去。 Go into the directory with your tests and do: 通过测试进入目录并执行以下操作:

$ nosetests testfile $耐心测试文件

(no .py on the end of testfile) (测试文件末尾没有.py)

You can also use the python unittest module: 您还可以使用python unittest模块:

$ python -m unittest testfile $ python -m unittest测试文件

I figured out what the problem was. 我发现了问题所在。 I had a non-test function which included 'test' in its name even it id did not start with 'test_', as a result of which it was treated as a test function by nose. 我有一个非测试函数,即使它的id不是以“ test_”开头,它的名称中也包含了“ test”,因此被鼻子当作测试函数。 When I modified the name of the function, the problem was solved. 当我修改函数名称时,问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM