我很好奇,如果可以在__main__
中初始化模块全局并在其使用pytest.main()
机制运行时使pytest可以访问全局模块,这种做法是否可行。
我最初的尝试似乎表明是否将全局状态传递给pytest会话...
import pytest
globalList = []
def test_globalList() :
global globalList
print( "globalList: {}".format( globalList))
assert globalList
if __name__ == '__main__':
print('Main Start')
globalList.append(1)
globalList.append(2)
globalList.append(3)
pytest_options = [ __file__ ]
print( "globalList: {}".format( globalList))
print('\n\n\n\n')
pytest.main( pytest_options ) # run pytest for just this file
print('Main Complete')
我产生以下控制台输出
Main Start
globalList: [1, 2, 3]
=================================== test session starts ================
platform darwin -- Python 2.7.10, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: /Users/USER/, inifile:
collected 1 item
test_temp.py F [100%]
=================================== FAILURES ===========================
___________________________________ test_globalList ____________________
def test_globalList() :
print( "globalList: {}".format( globalList))
> assert globalList
E assert []
test_temp.py:39: AssertionError
----------------------------------- Captured stdout call ---------------
globalList: []
=================================== 1 failed in 0.09 seconds ===========
Main Complete
我无法理解pytest中的哪个概念,这会提前提醒我这种尝试是不可能的。有没有一种方法可以在pytest.main
调用中共享全局状态?
证明globalList是一个全新的实例,但是在这两种情况下,进程和线程都是相同的。在与 main 上下文完全分开的pytest上下文下发生模块加载。引用documentation
中的注释注意
调用pytest.main()将导致导入您的测试及其导入的任何模块。
import pytest
import os
import threading
globalList = []
print( "\n__name__ : {}".format( __name__ ))
print("process ID : {}".format(hex(os.getpid())))
cur_thread = threading.current_thread()
print( "thread ID : {}".format( hex(id(cur_thread )) ))
print( "thread Name : {}".format( cur_thread.name ))
print( "globalList ID : {}".format(hex(id(globalList))))
def test_globalList() :
global globalList
print( "globalList: {}".format( globalList)) # TODO this will be empty in the pytest session, somehow this approach fails to bring this variable into scope.
assert globalList
if __name__ == '__main__':
print('Main Start')
globalList.append(1)
globalList.append(2)
globalList.append(3)
pytest_options = [ __file__ , '-s']
print( "globalList: {}".format( globalList))
print('\n\n\n\n')
pytest.main( pytest_options ) # run pytest for just this file
print('Main Complete')
以及相应的控制台输出...
__name__ : __main__
process ID : 0xa43e
thread ID : 0x10ac9b190
thread Name : MainThread
globalList ID : 0x10b476320
Main Start
globalList: [1, 2, 3]
=================================== test session starts ================
platform darwin -- Python 2.7.10, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: /Users/USER, inifile:
plugins: lazy-fixture-0.5.1, html-1.20.0, metadata-1.8.0
collecting ...
__name__ : test_temp
process ID : 0xa43e
thread ID : 0x10ac9b190
thread Name : MainThread
globalList ID : 0x10bb46128
collected 1 item
test_temp.py globalList: []
F
=================================== FAILURES ===========================
___________________________________ test_globalList ____________________
def test_globalList() :
global globalList
print( "globalList: {}".format( globalList))
> assert globalList
E assert []
test_temp.py:50: AssertionError
=================================== 1 failed in 0.10 seconds ===========
Main Complete
立即弹出一些差异,列表的模块名称和地址不同...
__name__ : __main__ test_temp <--different
process ID : 0xa43e 0xa43e
thread ID : 0x10ac9b190 0x10ac9b190
thread Name : MainThread MainThread
globalList ID : 0x10b476320 0x10bb46128 <--different