python Nose和nose2备选方案

fnatzsnv  于 2022-12-10  发布在  Python
关注(0)|答案(1)|浏览(140)

我一直在研究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我会永远感激。

eaf3rand

eaf3rand1#

我曾经使用鼻子,因为它提供了方便的覆盖测量。
现在我为python -m unittest *.py编写(使用import unittest),并经常使用pytest执行。
通常,我的CI/CD将以如下方式运行它:

pytest --cov --cov-report=term-missing

当然,它会执行递归发现,查找具有“test”名称的内容。
以下列方式开始文件:

import unittest

class TestFoo(unittest.TestCase):

    def test_something(self):

assert_equal调用变成了self.assertEqual调用,如果你想保留assert_equal的拼写,也可以写一个简短的实用函数来连接它们。
如果选择不使用import unittest,则可能需要定义以下简单实用函数:

def assert_equal(x, y):
    assert x == y

相关问题