学习编码,进行鼻子测试时出现语法错误,我看不到错误
Learning to code and a syntax error appears when I conduct nose tests, I can't see the error
我在以下代码中使用 nosetests 时收到语法错误。
我检查了代码是否与书中的描述相符,没有发现任何错误。这是我正在使用的代码段:
from nose.tools import *
from ex47.game import Room
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 can 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_eqaul(start.go('west'), west)
assert_equal(start.go('west').go('east'), start)
assert_equal(start.go('down').go('up'), start)
这是预期的结果:
$nosetests
...
Ran 3 tests in 0.008s
OK
这是我得到的实际结果:
E
======================================================================
ERROR: Failure: SyntaxError (invalid syntax (ex47_tests.py, line 9))
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/loader.py", line 417, in loadTestsFromName
addr.filename, addr.module)
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/Users/alasdairkite/anaconda3/lib/python3.7/imp.py", line 234,
in load_module
return load_source(name, filename, file)
File "/Users/alasdairkite/anaconda3/lib/python3.7/imp.py", line 171,
in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 696, in _load
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 724, in
exec_module
File "<frozen importlib._bootstrap_external>", line 860, in get_code
File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/alasdairkite/Documents/Programming/LMPTHW/LPTHW/projects/Ex47/skeleton/tests/ex47_tests.py", line 9
assert_equal(gold.name, "GoldRoom")
^
SyntaxError: invalid syntax
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (errors=1)
缺少括号。试试这个:
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, [])
我在以下代码中使用 nosetests 时收到语法错误。
我检查了代码是否与书中的描述相符,没有发现任何错误。这是我正在使用的代码段:
from nose.tools import *
from ex47.game import Room
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 can 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_eqaul(start.go('west'), west)
assert_equal(start.go('west').go('east'), start)
assert_equal(start.go('down').go('up'), start)
这是预期的结果:
$nosetests
...
Ran 3 tests in 0.008s
OK
这是我得到的实际结果:
E
======================================================================
ERROR: Failure: SyntaxError (invalid syntax (ex47_tests.py, line 9))
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/loader.py", line 417, in loadTestsFromName
addr.filename, addr.module)
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/Users/alasdairkite/anaconda3/lib/python3.7/site-
packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/Users/alasdairkite/anaconda3/lib/python3.7/imp.py", line 234,
in load_module
return load_source(name, filename, file)
File "/Users/alasdairkite/anaconda3/lib/python3.7/imp.py", line 171,
in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 696, in _load
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 724, in
exec_module
File "<frozen importlib._bootstrap_external>", line 860, in get_code
File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/alasdairkite/Documents/Programming/LMPTHW/LPTHW/projects/Ex47/skeleton/tests/ex47_tests.py", line 9
assert_equal(gold.name, "GoldRoom")
^
SyntaxError: invalid syntax
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (errors=1)
缺少括号。试试这个:
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, [])