Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何运行在发送Ctrl+之前不会返回的命令行程序;我同意_Python_Unit Testing_Rest_Tomcat7_Atlassian Plugin Sdk - Fatal编程技术网

在python中,如何运行在发送Ctrl+之前不会返回的命令行程序;我同意

在python中,如何运行在发送Ctrl+之前不会返回的命令行程序;我同意,python,unit-testing,rest,tomcat7,atlassian-plugin-sdk,Python,Unit Testing,Rest,Tomcat7,Atlassian Plugin Sdk,我正在编写python单元测试,针对需要作为另一个进程运行的RESTAPI进行测试 REST服务器是一个tomcat应用程序,我从shell调用它以开发模式运行,因此我希望在python测试中执行以下操作: 启动服务器,在服务器启动时返回 运行单元测试 发送服务器Ctrl+D,使其正常关闭 有没有一种方法可以使用python的单个入口点,以便服务器从一个python脚本调用启动并运行单元测试 我已经研究了python子进程和python中的多线程,但我仍然不太明白如何从这里开始 对于那些熟悉的人

我正在编写python单元测试,针对需要作为另一个进程运行的RESTAPI进行测试

REST服务器是一个tomcat应用程序,我从shell调用它以开发模式运行,因此我希望在python测试中执行以下操作:

  • 启动服务器,在服务器启动时返回
  • 运行单元测试
  • 发送服务器Ctrl+D,使其正常关闭 有没有一种方法可以使用python的单个入口点,以便服务器从一个python脚本调用启动并运行单元测试

    我已经研究了python子进程和python中的多线程,但我仍然不太明白如何从这里开始


    对于那些熟悉的人来说,这是我们正在开发的Atlassian JIRA插件,因此实际的shell命令是“atlas run”。

    由于没有人提供任何代码来帮助解决这个问题,我将执行以下操作。原来
    pexpect
    功能非常强大,您不需要
    信号
    模块

    import os
    import sys
    import pexpect
    
    def run_server():
        server_dir = '/path/to/server/root'
        current_dir = os.path.abspath(os.curdir)
    
        os.chdir(server_dir)
        server_call = pexpect.spawn('atlas-run')
        server_response = server_call.expect(['Server Error!', 'Sever is running!'])
        os.chdir(current_dir)
        if server_response:
            return server_call #return server spawn object so we can shutdown later
        else:
            print 'Error starting the server: %s'%server_response.after
            sys.exit(1)
    
    def run_unittests():
        # several ways to do this. either make a unittest.TestSuite or run command line
        # here is the second option
        unittest_dir = '/path/to/tests'
        pexpect.spawn('python -m unittest discover -s %s -p "*test.py"'%unittest_dir)
        test_response = pexpect.expect('Ran [0-9]+ tests in [0-9\.]+s') #catch end
        print test_response.before #print output of unittests before ending.
        return
    
    def main():
        server = run_sever()
        run_unittests()
        server.sendcontrol('d') #shutdown server
    
    if __name__ == "__main__":
        main()
    

    1.)如何从命令行调用服务器?您可以使用
    pexpect
    模块吗?2.)服务器启动后,只需运行
    unittest.main()
    。3.)您可以使用
    信号
    模块将
    SIGQUIT
    pexpect
    捕获到服务器上吗?我不知道pexpect,我现在正在研究,谢谢!通过CDing到插件项目的根目录并从命令行运行“atlasrun”来调用服务器。这很有效!我选择TestSuite路线,而不是在另一个进程中运行单元测试,但仍然是一个非常棒的答案。谢谢