2012-10-23 58 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對象的方法。

好狩獵。

相關問題