LoginSignup
4
5

More than 5 years have passed since last update.

Pythonでexeテスト実行

Posted at

背景

exeファイル作っていくつかあるテストケースを実行して確認しようという時に、
「これ、一括でテスト実行できないかな?」と思ったのが発端です。

構成

  • Windowsを想定しています。
  • exeの名前は「sample.exe」とします。
  • exeにはPATHが通ってるものとします。
  • exeを実行すると「result.txt」というファイルが出力されます。
  • テストケースは下記構造になっているものとします。

C:\test\TestCase01
C:\test\TestCase02
C:\test\TestCase03

実行

import os
import subprocess

exe_name = 'sample.exe'
curr_dir = 'C:\\test\\'
sub_dirs = [d for d in os.listdir(curr_dir)]
for sub_dir in sub_dirs:
    os.chdir(os.path.join(curr_dir, sub_dir))
    # 実行状況確認用出力
    print 'Execute : ' + sub_dir
    # exeファイル実行
    subprocess.call(exe_name)
    os.chdir('../')

テスト一括処理スクリプトの完成です。

データ差分確認

exeファイルを更新した時に、
実行前と実行後で差分が出ていないか心配になりますよね?
そんな時にこんなスクリプトが役に立ちます。

import glob
import filecmp

# 更新前テスト実行結果
old_dir = 'C:\\test\\old'
# 更新後テスト実行結果
new_dir = 'C:\\test\\new'
# 比較対象ファイル指定
diff_file = os.path.join(new_dir, 'TestCase*/result.txt')
# 差分確認
match, mismatch, errors = filecmp.cmpfiles(
    old_dir, new_dir, glob.glob(diff_file))

matchには一致したファイルがリストに格納されております。
mismatchは不一致ファイルがリストに格納されており、
errorsはファイルが存在しないなど、
差分確認できないファイルがリストに格納されております。

余談

ぶっちゃけ差分確認ツール使えばできるんですけどね(笑)

ただツールだとexeで複数ファイルが出力された場合、
「各フォルダのファイルAのみを比較したい」
なんて時に、
「このフォルダ差分出てるけど、差分出てるの他のファイルじゃん」
って時にglobで指定できるから楽なんです。

ツールに条件設定すれば…

最後にツールも起動させちゃいましょう。

import subprocess
cmd = '[差分確認ツール(WinMergeなど)] ' + old_dir + ' ' + new_dir
subprocess.call(cmd)

これでmismatchにリストアップされたファイルを確認することができます。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5