我目前正在使用python 2.7中的pytest进行一组集成测试,这些测试执行以下操作:
1)在本地计算机上后台运行服务器二进制文件
2)向服务器发送请求并验证结果
3)终止后台服务器进程
一切似乎都在正常工作,除了我无法终止在我的计算机上运行的服务器进程。{{cd2>似乎要在我的计算机上运行了。在
AttributeError: 'NoneType' object has no attribute 'terminate'
你有什么想法吗?我错过了什么明显的东西吗?在
import time
import subprocess
server_background_process_pipe = None
def setup_module():
# Start the test server in the background
cmd = 'bin/my_server --key1='+value1+' --key2='+value2+' &' # The '&' tells my bin to run in the background
server_background_process_pipe = subprocess.Popen(cmd, shell=True,stderr=subprocess.STDOUT)
print(server_background_process_pipe) # prints '<subprocess.Popen object at 0x10aabd250>'
time.sleep(1) # Wait for the server to be ready
def test_basic_get_request():
print(server_background_process_pipe) # prints 'None'
response = send_request_to_server()
fail_if_not_as_expected(response) # Response is exactly as expected
def teardown_module():
# kill the server that was launched in setup_module to serve requests in the tests
# AttributeError: 'NoneType' object has no attribute 'terminate'
server_background_process_pipe.terminate()
额外信息:
即使服务器进程仍在运行,它仍然是None
。它是None
,而测试正在运行。它在测试套件完成后运行很长时间。如果我重新运行测试,我会在控制台中收到一条消息,说明我的服务器部署失败,因为它已经在运行。测试仍然可以通过,因为它们从上一次执行中向服务器发送请求。在
由于服务器需要在后台运行,所以我使用子流程.Popen构造函数,而不是像check_output
这样的方便方法之一。在
在
^{pr2}$server_background_process_pipe
是一个局部变量。它从来没有分配给globalserver_background_process_pipe
,所以globalserver_background_process_pipe
总是None
和代码尝试从None获取属性
terminate
。在您需要的是对全局变量进行初始赋值:
相关问题 更多 >
编程相关推荐