1
1

More than 1 year has passed since last update.

2つのファイルのDiffを取るときの備忘録

Last updated at Posted at 2022-07-22

概要

  • 2つのファイルのDiffを取って違った場合にFAILEDになるテストスクリプトを作りたい
  • 比較する2つのファイル名が同じである
  • 比較する2つのファイル名が同じ場合、WinMergeで同じことができるがPCがMacのためスクリプトを書くことにした

参考

環境

# Pythonのバージョンを確認
python --version
> Python 3.7.3

# pytestのインストール
> pip install pytest

# natsortのインストール
> pip install natsort

実装

test_compare.py
import os
import difflib
import glob
from natsort import natsorted
import pytest
import re


folder_1 = "/filepath/"
folder_2 = "/filepath/"

files_1 = natsorted(glob.glob(os.path.join(folder_1, "*.txt")))
files_2 = natsorted(glob.glob(os.path.join(folder_2, "*.txt")))



def get_testdata():

    testdata = []
    testdata_ids = []
    for filename_with_path in files_1:
        filename = os.path.basename(filename_with_path)
        filename = filename.split('.')[0]
        testdata.append(filename_with_path)
        testdata_ids.append(f'{filename}')
    
    return [testdata, testdata_ids]

testdata = get_testdata()[0]
testdata_ids = get_testdata()[1]


@pytest.mark.parametrize("id", testdata_ids)
#@pytest.mark.parametrize("id", range(len(files_1))) # [0, 1, 2, 3, 4]
def test_compare(id):
    
    target_file_1 = f"{folder_1}/{id}.txt"
    target_file_2 = f"{folder_2}/{id}.txt" 

    if not os.path.exists(target_file_1) or not os.path.exists(target_file_2):
        raise FileNotFoundError

    f_p = open(target_file_1).read()
    f_d = open(target_file_2).read()

    diff = difflib.unified_diff(f_p.split(), f_d.split())
    diff_str = '\n'.join(diff)
    # print(diff_str)

    assert not diff_str, f"{target_file_1}, {target_file_2} FAILED!!"


結果

============================================================================================= test session starts =============================================================================================
platform darwin -- Python 3.7.3, pytest-7.1.2, pluggy-0.13.1
rootdir: /Users/test
plugins: cov-2.12.1, freezegun-0.4.2, Faker-5.3.0, mock-3.7.0
collected 304 items                                                                                                                                                                                           

test_compare.py ....................................................................................................................................................................................... [ 60%]
.........................................................................................................................                                                                               [100%]

============================================================================================= 304 passed in 1.10s =============================================================================================
1
1
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
1
1