我一直在研究Learn Python 3 The Hard way by Zed Shaw
,最近遇到了一个大问题。所以这本书本身已经过时了,书中包含的例子使用了nose
模块,显然现在已经不支持了。我通过乏味的研究设法通过了这些例子中的大多数。但是我现在在第202页,Zed导入了下面的模块from nose.tools import *
,它使他能够使用assert
(不管它做什么),我不能为了我自己的缘故,在nose2
中找到一个同样启用该功能的等效导入
代码如下:
from nose2.tools import *
from ex47.game import Room
def test_room():
gold = Room("GoldRoom",
"""This room has gold in it yo
u can grab. There's a door to the north""")
assert_equal(gold.name, "GoldRoom")
assert_equal(gold.paths, {})
def test_room_paths():
center = Room("Center", "Test room in the center.")
north = Room("North", "Test room in the north.")
south = Room("South", "Test room in the south.")
center.add_paths({"north": north, "south": south})
assert_equal(center.go("north"), north)
assert_equal(center.go("south"), south)
def test_map():
start = Room("Start", "you can go west and down the hole.")
west = Room("Trees", "There are trees here, you can go east.")
down = Room("Dungeon", "It\'s dark down here, you can go up.")
start.add_paths({"west": west, 'down': down})
west.add_paths({'east': start})
down.add_paths({'up': start})
assert_equal(start.go('west'), west)
assert_equal(start.go('west').go('east'), start)
assert_equal(start.go("down").go('up'), start)
如果有人可以请给予我一个替代进口nose2
我会永远感激。
1条答案
按热度按时间eaf3rand1#
我曾经使用鼻子,因为它提供了方便的覆盖测量。
现在我为
python -m unittest *.py
编写(使用import unittest
),并经常使用pytest
执行。通常,我的CI/CD将以如下方式运行它:
当然,它会执行递归发现,查找具有“test”名称的内容。
以下列方式开始文件:
assert_equal
调用变成了self.assertEqual
调用,如果你想保留assert_equal
的拼写,也可以写一个简短的实用函数来连接它们。如果选择不使用
import unittest
,则可能需要定义以下简单实用函数: