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

nosetests.exe calling setup_class() but pycharm unit tests aren't

我正在尝试移植 SqlSoup to python 3. I'm using PyCharm as my IDE and I want to run the unit tests

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

C:\bin\python\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.3\helpers\pycharm\utrunner.py" C:\Users\jdearing\Documents\deleteme\sqlsoup\tests\test_sqlsoup.py true
Testing started at 12:53 PM ...

Error
Traceback (most recent call last):
  File "C:\Users\jdearing\Documents\deleteme\sqlsoup\tests\test_sqlsoup.py", line 25, in setUp
    engine.execute(sql)
NameError: name 'engine' is not defined

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

C:\Users\jdearing\Documents\deleteme\sqlsoup>c:\bin\python\Scripts\nosetests-3.4.exe tests\test_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 以满足依赖性,因此我假设它的测试运行器正在使用该模块而不是其他模块。为什么会给出不同的结果?

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

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