0

I have been working my way through Learn Python 3 The Hard way by Zed Shaw and I have recently encountered a huge issue. So the book itself is outdated and examples included in the book make use of the nose module which apparently isn't supported anymore. I managed to make my way through most of these examples by tedious research, but I'm now on page 202 and Zed imports the following module from nose.tools import *which enables him to use assert(whatever that does) and I cannot for the sake of me, find an equivalent import in nose2 that would also enable that function

here's the code:

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)

If someone could please give me an alternative import in nose2 i'd be forever grateful.

  • "So the book itself is outdated"—a good reason not to use it. There are plenty of modern resources that would be better. – ChrisGPT was on strike Dec 09 '22 at 17:20
  • "which enables him to use `assert` (whatever that does)"—[`assert`](https://docs.python.org/reference/simple_stmts.html#index-18) is available as part of Python itself. Do you mean `assert_equal()` etc.? – ChrisGPT was on strike Dec 09 '22 at 17:22
  • yeah, that's exactly it @chris –  Dec 09 '22 at 17:25
  • [The built-in `unittest` module](https://docs.python.org/library/unittest.html) provides [a bunch of assert methods](https://docs.python.org/library/unittest.html#assert-methods). Or you could just `assert center.go("south") == south`. – ChrisGPT was on strike Dec 09 '22 at 17:25

1 Answers1

0

I used to use nose, as it offered convenient coverage measurements.

Nowadays I author for python -m unittest *.py (using import unittest) and will often execute with pytest.

Typically my CI/CD will run it this way:

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

and of course that does recursive discovery, looking for things with "test" names.


Start your file this way:

import unittest

class TestFoo(unittest.TestCase):

    def test_something(self):

The assert_equal calls become self.assertEqual calls. Or write a short utility function to connect them if you want to preserve that assert_equal spelling.


If you choose to not import unittest, you might want to define this trivial utility function:

def assert_equal(x, y):
    assert x == y
J_H
  • 17,926
  • 4
  • 24
  • 44