繁体   English   中英

Python-Nosetest返回名称* class *仅在多个文件中的一个文件上未定义

[英]Python - Nosetest returning name *class* not defined on only one file of many

我正在尝试使用鼻子测试测试一组python文件,并遇到“ NameError:未定义名称'句子'”(请注意,句子是我要在a文件中测试的类的名称)。 对于其他正在测试的文件/类,我没有收到此错误,并且似乎没有任何循环依赖问题。

错误详细信息在这里:

ex48 $ nosestests
......E
======================================================================
ERROR: Failure: NameError (name 'Sentence' is not defined)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Users/valentinblehaut/LearnPyth/ex48/tests/parser_tests.py", line 6, in <module>
    stest = Sentence()
NameError: name 'Sentence' is not defined

----------------------------------------------------------------------
Ran 7 tests in 0.007s

FAILED (errors=1)

这是我的文件夹结构以及测试(parser_tests.py)和已测试(parser.py)文件的参考:

资料夹结构:

+ ex48
    + bin
    + docs
    - lexicon.py
    - parser.py
    - setup.py
    + tests
            - __init__.py
            - lexicon_tests.py
            - parser_tests.py

parser.py文件:

class Sentence(object):

    def __init__(self, subject, verb, object):
        self.subject = subject[1]
        self.verb = verb[1]
        self.object = object[1]

parser.py的测试脚本:

from nose.tools import *
from parser import *

# first create an instance of Sentence class called stest
stest = Sentence()

def test_Sentence():
    assert_equal(stest.subject, "Hi")
    assert_equal(stest.verb, "Hello")
    assert_equal(stest.object, "Howdy")

我也尝试过仅测试parser.py与:

ex48 $ nostests parser.py

这将返回相同的NameError消息。

对于任何格式/约定错误,请提前致歉,敬请告知我以后需要解决的任何问题。

parser.py可能会与具有相同名称的python包引起冲突,是否可以将parser.py替换为其他名称并在测试脚本导入中进行更新。

暂无
暂无

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

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