我正在尝试从《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)
最佳答案
我认为您应该在这里访问顶级模块。例如,在您的 __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/