fix: pytestやpytest-htmlのバージョンが上がったことに伴い、単体テストが動かなくなっていたのを修正。また、s3のテストが元のモジュールの修正が適用されていなかったため、修正した。

This commit is contained in:
shimoda.m@nds-tyo.co.jp 2024-08-21 15:57:28 +09:00
parent dd891aa548
commit 39fd1383bd
2 changed files with 20 additions and 20 deletions

View File

@ -1,7 +1,8 @@
import os
import pytest
from src.aws.s3 import BackupBucket, ConfigBucket, DataBucket, S3Resource
from src.aws.s3 import BackupBucket, ConfigBucket, DataBucket, S3Client
@pytest.fixture
@ -15,7 +16,7 @@ def s3_test(s3_client, bucket_name):
yield
class TestS3Resource:
class TestS3Client:
def test_get_object(self, s3_test, s3_client, bucket_name):
"""
@ -31,7 +32,7 @@ class TestS3Resource:
s3_client.put_object(Bucket=bucket_name, Key='hogehoge/test.txt', Body=b'aaaaaaaaaaaaaaa')
# Act
sut = S3Resource(bucket_name)
sut = S3Client(bucket_name)
actual = sut.get_object('hogehoge/test.txt')
# Assert
@ -48,7 +49,7 @@ class TestS3Resource:
"""
# Arrange
# Act
sut = S3Resource(bucket_name)
sut = S3Client(bucket_name)
with pytest.raises(Exception):
# Assert
sut.get_object('hogehoge/test.txt')
@ -68,7 +69,7 @@ class TestS3Resource:
with open(file_path, mode='w') as f:
f.write('aaaaaaaaaaaaaaa')
sut = S3Resource(bucket_name)
sut = S3Client(bucket_name)
sut.put_object('hogehoge/test.txt', file_path)
actual = s3_client.get_object(Bucket=bucket_name, Key='hogehoge/test.txt')
@ -87,7 +88,7 @@ class TestS3Resource:
"""
# Arrange
# Act
sut = S3Resource(bucket_name)
sut = S3Client(bucket_name)
with pytest.raises(Exception):
# Assert
sut.put_object('hogehoge/test.txt', 'aaaaaaaaaaaaaaa')
@ -108,7 +109,7 @@ class TestS3Resource:
s3_client.create_bucket(Bucket=for_copy_bucket)
s3_client.put_object(Bucket=bucket_name, Key='hogehoge/test.txt', Body=b'aaaaaaaaaaaaaaa')
sut = S3Resource(bucket_name)
sut = S3Client(bucket_name)
sut.copy(bucket_name, 'hogehoge/test.txt', for_copy_bucket, 'test.txt')
actual = s3_client.get_object(Bucket=for_copy_bucket, Key='test.txt')
@ -125,7 +126,7 @@ class TestS3Resource:
"""
# Arrange
# Act
sut = S3Resource(bucket_name)
sut = S3Client(bucket_name)
with pytest.raises(Exception):
# Assert
sut.copy(bucket_name, 'hogehoge/test.txt', 'for_copy_bucket', 'test.txt')
@ -140,7 +141,7 @@ class TestS3Resource:
- インスタンス生成時に例外が発生すること
"""
with pytest.raises(Exception) as e:
S3Resource()
S3Client()
assert e.value.args[0] == "__init__() missing 1 required positional argument: 'bucket_name'"

View File

@ -4,8 +4,7 @@ from datetime import datetime
import boto3
import pytest
from moto import mock_s3
from py.xml import html # type: ignore
from moto import mock_aws
from . import docstring_parser
@ -21,7 +20,7 @@ def aws_credentials():
@pytest.fixture
def s3_client(aws_credentials):
with mock_s3():
with mock_aws():
conn = boto3.client("s3", region_name="us-east-1")
yield conn
@ -35,18 +34,18 @@ def pytest_html_report_title(report):
def pytest_html_results_table_header(cells):
del cells[2:]
cells.insert(3, html.th("Cases"))
cells.insert(4, html.th("Arranges"))
cells.insert(5, html.th("Expects"))
cells.append(html.th("Time", class_="sortable time", col="time"))
cells.insert(3, '<th>Cases</th>')
cells.insert(4, '<th>Arranges</th>')
cells.insert(5, '<th>Expects</th>')
cells.append('<th class="sortable time" col="time">Time</th>')
def pytest_html_results_table_row(report, cells):
del cells[2:]
cells.insert(3, html.td(html.pre(report.cases))) # 「テスト内容」をレポートに出力
cells.insert(4, html.td(html.pre(report.arranges))) # 「期待結果」をレポートに出力
cells.insert(5, html.td(html.pre(report.expects))) # 「期待結果」をレポートに出力
cells.append(html.td(datetime.now(), class_="col-time")) # ついでに「時間」もレポートに出力
cells.insert(3, f'<td><pre>{report.cases}</pre></td>') # 「テスト内容」をレポートに出力
cells.insert(4, f'<td><pre>{report.arranges}</pre></td>') # 「期待結果」をレポートに出力
cells.insert(5, f'<td><pre>{report.expects}</pre></td>') # 「期待結果」をレポートに出力
cells.append(f'<td class="col-time">{datetime.now()}</td>') # ついでに「時間」もレポートに出力
@pytest.hookimpl(hookwrapper=True)