我正在学习 python,但在尝试运行一些简单的单元测试时遇到了一些麻烦。我nosetests
用来运行一些手动编写的测试,并且我已经使用 Travis 持续集成设置了我的 github 存储库,以进行一些 CI 实践。我注意到它对 python 3.x 的所有构建都失败了,但是为 2.x 传递了任何东西。
首先,这是我的两个文件。
fib.py
#! /usr/bin/python3
#fib.py
import sys
def fib(n):
if (n == 0):
return 0
if (n == 1):
return 1
return fib(n -1) + fib(n - 2)
def main():
if (len(sys.argv) == 2):
print(fib(sys.argv[1]))
else:
print("Usage: ", sys.argv[0], " [number]")
if __name__ == "__main__":
main()
test_fib.py
#! /usr/bin/python3
#testfib.py
import unittest
from fib import fib
class TestFib(unittest.TestCase):
def test_fib_0(self):
self.assertEqual(fib(0), 0)
def test_fib_1(self):
self.assertEqual(fib(1), 1)
def test_fib_2(self):
self.assertEqual(fib(2), 1)
def test_fib_range(self):
for x in range(2,13):
self.assertEqual(fib(x), fib(x - 1) + fib(x - 2))
if __name__ == "__main__":
unittest.main()
运行 python test_fib.py
:
....
----------------------------------------------------------------------
Ran 4 tests in 0.018s
OK
跑步nosetests
....
----------------------------------------------------------------------
Ran 4 tests in 0.053s
OK
pi@kepler:~/dev/Projects/Numbers/fib$
但是,当我部署到 Travis CI 并nosetests
使用 python3.x 运行,或者我nosetests3
在我的机器上运行时,我看到了:
EEEE
======================================================================
ERROR: test_fib_0 (fib.test_fib.TestFib)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/dev/Projects/Numbers/fib/test_fib.py", line 9, in test_fib_0
self.assertEqual(fib(0), 0)
TypeError: 'module' object is not callable
======================================================================
ERROR: test_fib_1 (fib.test_fib.TestFib)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/dev/Projects/Numbers/fib/test_fib.py", line 12, in test_fib_1
self.assertEqual(fib(1), 1)
TypeError: 'module' object is not callable
======================================================================
ERROR: test_fib_2 (fib.test_fib.TestFib)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/dev/Projects/Numbers/fib/test_fib.py", line 15, in test_fib_2
self.assertEqual(fib(2), 1)
TypeError: 'module' object is not callable
======================================================================
ERROR: test_fib_range (fib.test_fib.TestFib)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/dev/Projects/Numbers/fib/test_fib.py", line 19, in test_fib_range
self.assertEqual(fib(x), fib(x - 1) + fib(x - 2))
TypeError: 'module' object is not callable
----------------------------------------------------------------------
Ran 4 tests in 0.034s
FAILED (errors=4)
我对python很陌生,所以我不确定我的问题是我的代码还是我如何使用nose。关于我做错了什么的任何想法?
以下是我正在运行的所有内容的版本:
$ python -V
Python 2.7.3
$ python3 -V
Python 3.2.3
$ nosetests -V
nosetests version 1.3.0
$ nosetests3 -V
nosetests3 version 1.1.2
$ cat /etc/*-release
PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
NAME="Debian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support/"
BUG_REPORT_URL="http://bugs.debian.org/"