2012-10-23 28 views
3

我正在编写一个用于持续集成和测试的python脚本,该脚本将被咬人调用。我们的单元测试使用谷歌测试框架。每个软件组件都有一个运行配置和其他所需服务的bash脚本,并运行gtest可执行文件。 python脚本遍历存储库查找bash脚本,并使用os.popen()命令调用每个脚本。从python调用seg时出错,但从命令行正常运行

Python脚本(UnitTest.py)

#!/usr/bin/python 

import os 
import fnmatch 
import sys 
import subprocess 

repository_location = '/home/actuv/workspace/eclipse/iccs/' 
unit_test_script_name = 'RunUnitTests.sh' 

def file_locator(repo, script_name): 
    # Function for determining all unit test scripts 
    test_location = [] 
    for root, dirnames, filenames in os.walk(repo): 
     for filename in fnmatch.filter(filenames, script_name): 
      test_location.append(os.path.join(root)) 
    return test_location 

def run_tests(test_locations, script_name): 
    # Runs test scripts located at each test location 
    for tests in test_locations: 
     cmd = 'cd ' + tests + ';./' + script_name 
     print 'Running Unit Test at: ' + tests 
     os.popen(cmd) 

################ MAIN ################ 
# Find Test Locations 
script_locations = file_locator(repository_location, unit_test_script_name) 

# Run tests located at each location 
run_tests(script_locations) 

# End of tests 
sys.exit(0) 

bash脚本

#!/bin/sh 

echo "Running unit tests..." 

# update the LD_LIBRARY_PATH to include paths to our shared libraries 

# start the test server 

# Run the tests 

# wait to allow all processes to end before terminating the server 
sleep 10s 

当我从终端窗口手动运行bash脚本,它运行良好。当我使用python脚本调用bash脚本时,我在bash脚本的TestSingleClient和TestMultiClientLA行上出现了分段错误。

回答

2

尝试用

proc = subprocess.Popen('./scriptname', shell = True, 
         cwd = tests) 
proc.communicate() 
+0

我最初有一个问题,它找不到脚本,但我意识到我需要在脚本名称前加'./'。 – Axe

+0

啊,我明白了。编辑... – unutbu

+1

我还发现,当允许管道传输到被咬的控制台时,stdout和stderr会引起重大问题。我打开了一个临时文件(temp = open(temp,'w')),并将stdout = temp和stderr = temp标志添加到Popen调用中。 – Axe

1

替换

os.popen(cmd) 

肯定检查出subprocess模块 - 具体看subprocess.call()简便方法。我扔了一个os.path检查,以确保您的测试目录也存在。

def run_tests(test_locations, script_name): 
    # Runs test scripts located at each test location 
    for tests in test_locations: 
     # Make sure tests directory exists and is a dir 
     if os.path.isdir(tests): 
      print 'Running Unit Test at: ' + tests 
      subprocess.call(script_name, shell=True, cwd=tests) 

而且 - 你在你关于标准输出正确的观察和标准错误导致的问题,尤其是当有大量的数据。当存在大量或未知量的输出时,我使用stdout/stderr的临时文件。例子:
Ex。

def execute_some_command(cmd="arbitrary_exe"): 
    """ Execute some command using subprocess.call()""" 
    # open/create temportary file for stdout  
    tmp_out=file('.tmp','w+') 

    # Run command, pushing stdout to tmp_out file handle 
    retcode = subprocess.call(cmd, stdout=tmp_out, shell=True) 

    if retcode != 0: 
     # do something useful (like bailout) based on the OS return code 
     print "FAILED" 

    # Flush any queued data 
    tmp_out.flush() 
    # Jump to the beginning 
    tmp_out.seek(0) 
    # Parse output 
    for line in tmp_out.readlines(): 
     # do something useful with output 

    # cleanup 
    tmp_out.close() 
    os.remove(_out.name) 
return  

检查出如何从处理您的标准输出数据_out文件句柄蟒蛇file对象的方法。

好狩猎。

相关问题