nosetests.exe调用setup_class()但pycharm单元测试不是



我正在尝试将SqlSoup移植到python 3。我正在使用 PyCharm 作为我的 IDE,我想运行单元测试。

如果我在 pycharm 中运行单元测试,我会得到以下输出:

C:binpythonpython.exe "C:Program Files (x86)JetBrainsPyCharm Community Edition 4.0.3helperspycharmutrunner.py" C:UsersjdearingDocumentsdeletemesqlsoupteststest_sqlsoup.py true
Testing started at 12:53 PM ...
Error
Traceback (most recent call last):
  File "C:UsersjdearingDocumentsdeletemesqlsoupteststest_sqlsoup.py", line 25, in setUp
    engine.execute(sql)
NameError: name 'engine' is not defined

但是,如果我从命令行运行测试,一切正常:

C:UsersjdearingDocumentsdeletemesqlsoup>c:binpythonScriptsnosetests-3.4.exe teststest_sqlsoup.py
.............................
----------------------------------------------------------------------
Ran 29 tests in 0.549s
OK

根结底,此方法永远不会被调用:

    @classmethod
    def setup_class(cls):
        global engine
        engine = create_engine('sqlite://', echo=True)
        for sql in _ddl:
            engine.execute(sql)

是的,这是一个全局变量,我将在运行单元测试后对其进行改进。

Pycharm 要求我安装 nose 以满足依赖关系,所以我假设它的测试运行器正在使用该模块而不是另一个模块。为什么它会产生不同的结果?

据我所知,鼻子的设置必须是以下格式,以便在测试类开始时拾取和运行:

@classmethod 
def setUpClass(cls):
    global engine
    ...

最新更新