python - 艰难地学习 Python 练习 47 个 Nose 测试 NameError

标签 python python-3.x

我正在尝试从《Learn Python 3 the Hard way》一书中学习 Python 3,但尝试在练习 47 中使用 Nostests 运行测试时遇到错误,在顶级 ex47 目录中运行我的命令。

我的文件夹结构是:

ex47
  -bin
  -docs
  -ex47
      __init__.py
      game.py
  -test
      __init__.py
      ex47_tests.py
  setup.py

init 文件的内容为空。对于其他文件来说是: 游戏.py

class Room(object):

    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.paths = {}

    def go(self, direction):
        return self.paths.get(direction, None)

    def add_paths(self, paths):
        self.paths.update(paths)

设置.py

try:
    from setuptools import setup
except ImportError:
    from distutials.core import setup

config = {
    'decription' : 'My Project',
    'author' : 'My Name',
    'url' : 'URL to get it at.',
    'download_url' : 'Where to download it.',
    'author_email' : 'My Email',
    'version' : '0.1',
    'install_requires' : ['nose'],
    'packages' : ['ex47'],
    'scripts' : [],
    'name' : 'ex47'
}

setup(**config)

ex47_tests.py

from nose.tools import *
import ex47

def test_room():
    gold = Room("GoldRoom",
                """This room has gold in it you 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 van go west and down a 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)

尝试在 PowerShell 中运行此命令会出现与未找到 Room 类相关的错误。 enter image description here

最佳答案

我认为您应该在这里访问顶级模块。例如,在您的 __init__.py 中添加以下行:

__all__ = ["game"]

然后,像其他导入一样导入它

from game import Room

您也可以使用顶级目录:

from ex34.game import Room

或者用一种简单的方式,将所有文件放在一个目录中以便轻松访问它们

ex47
  game.py
  ex47_tests.py
  setup.py

关于python - 艰难地学习 Python 练习 47 个 Nose 测试 NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52003458/

相关文章:

python - 在从 Excel 范围填充的嵌套元组上使用 join()

python - 维护良好的 `conda` channel 列表

Python:如何将列表列表的元素转换为无向图?

python - 空列表的正确类型提示是什么?

python-2.7 - 性能 : Multiprocessing with shared object between Python 3. 5 和 2.7

python-3.x - 在pytorch中追溯不推荐使用的警告

Python 和 shell 命令

python - 使用 Django haystack MultiValueField 迭代搜索结果 View 中的项目

python - OSError : [WinError 123] The filename, 目录名,或卷标语法不正确: '<frozen importlib._bootstrap>' (Django)

python-3.x - 使用 cv2.imshow 时获取此黑色窗口而不是图片