繁体   English   中英

鼻子和鼻子2的替代品

[英]Nose and nose2 alternatives

我一直在Learn Python 3 The Hard way by Zed Shaw ,我最近遇到了一个大问题。 所以这本书本身已经过时了,书中包含的示例使用了显然不再受支持的nose模块。 我通过繁琐的研究设法完成了大部分示例,但我现在在第 202 页,Zed from nose.tools import *导入了以下模块,这使他能够使用assert (无论做什么)而我不能看在我的分上,在nose2中找到一个等效的导入,它也可以启用 function

这是代码:

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中给我一个替代导入,我将永远感激不已。

我曾经使用鼻子,因为它提供了方便的覆盖测量。

现在我为python -m unittest *.py (使用import unittest )创作并且经常使用pytest执行。

通常我的 CI/CD 会这样运行:

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

当然,这会进行递归发现,寻找具有“测试”名称的事物。


以这种方式开始你的文件:

import unittest

class TestFoo(unittest.TestCase):

    def test_something(self):

assert_equal调用变为self.assertEqual调用。 如果您想保留assert_equal拼写,或者编写一个简短的实用程序 function 来连接它们。


如果您选择不import unittest ,您可能需要定义这个简单的实用程序 function:

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

暂无
暂无

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

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