我正在为下一个函数编写测试:
def foo():
print 'hello world!'
Run Code Online (Sandbox Code Playgroud)
所以,当我想测试这个函数时,代码将是这样的:
import sys
from foomodule import foo
def test_foo():
foo()
output = sys.stdout.getline().strip() # because stdout is an StringIO instance
assert output == 'hello world!'
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用-s参数运行nosetests,测试会崩溃.如何通过unittest或nose模块捕获输出?
目前我的代码按以下树结构组织:
src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
Run Code Online (Sandbox Code Playgroud)
当module*.py
文件包含源代码和test_module*.py
包含TestCase
S为相关模块.
通过以下命令,我可以运行单个文件中包含的测试,例如:
$ cd src
$ nosetests test_filesystem.py
..................
----------------------------------------------------------------------
Ran 18 tests in 0.390s
OK
Run Code Online (Sandbox Code Playgroud)
我怎样才能运行所有测试?我试过nosetests -m 'test_.*'
但它不起作用.
$cd src
$ nosetests -m 'test_.*'
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Run Code Online (Sandbox Code Playgroud)
谢谢
我试图用鼻子编写测试,使用多处理计算得出的东西.
我有这个目录结构:
code/
tests/
tests.py
Run Code Online (Sandbox Code Playgroud)
tests.py看起来像这样:
import multiprocessing as mp
def f(i):
return i ** 2
pool = mp.Pool()
out = pool.map(f, range(10))
def test_pool():
"""Really simple test that relies on the output of pool.map.
The actual tests are much more complicated, but this is all
that is needed to produce the problem."""
ref_out = map(f, range(10))
assert out == ref_out
if __name__ == '__main__':
test_pool()
Run Code Online (Sandbox Code Playgroud)
从code
目录运行,python tests/tests.py
传递.
nosetests tests/tests.py
未能完成.它启动,但永远不会通过呼叫pool.map …