3

私は、bitten によって呼び出される継続的な統合とテストに使用される python スクリプトを作成しています。単体テストでは、Google テスト フレームワークを使用します。各ソフトウェア コンポーネントには、構成およびその他の必要なサービスを実行し、gtest 実行可能ファイルを実行する bash スクリプトがあります。Python スクリプトは、bash スクリプトを探してリポジトリを調べ、os.popen() コマンドを使用して各スクリプトを呼び出します。

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)

バッシュスクリプト

#!/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 行でセグメンテーション エラーが発生します。

4

2 に答える 2

2

交換してみてください

os.popen(cmd)

proc = subprocess.Popen('./scriptname', shell = True, 
                       cwd = tests)
proc.communicate()
于 2012-10-23T00:37:46.663 に答える
1

サブプロセスモジュールを必ずチェックしてください。具体的には、 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に関する観察結果は正しいです。出力の量が多いか不明な場合は、stdout/stderrに一時ファイルを使用します。
元。

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ファイルハンドル からstdoutデータを処理する方法については、Pythonファイルオブジェクトのメソッドを確認してください。

良い狩り。

于 2012-11-09T22:28:23.783 に答える