Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor existing tox test to pytest #123

Merged
merged 13 commits into from
Oct 3, 2024
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ tox
pytest
pytest-cov
pytest-flake8
pytest-xdist
flake8==3.9.2
54 changes: 54 additions & 0 deletions tests/file_format_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0

import os
import subprocess
import shutil
import pytest
import tempfile


@pytest.fixture(scope="function", autouse=True)
def setup_test_result_dir_and_teardown():
# 각 테스트마다 임시 디렉토리 생성
test_result_dir = tempfile.mkdtemp(dir=".") # 고유한 임시 디렉토리 생성
print("==============setup: {test_result_dir}==============")

yield test_result_dir # 임시 디렉토리 경로를 테스트 함수로 넘김

print("==============tearDown==============")
shutil.rmtree(test_result_dir)


def run_command(*args): # 리눅스 명령어 실행
result = subprocess.run(args, capture_output=True, text=True)
success = result.returncode == 0
output = result.stdout if success else result.stderr
return success, output


@pytest.mark.parametrize("file_format, extension", [
("excel", ".xlsx"),
("csv", ".csv"),
("opossum", ".json"),
("yaml", [".yaml", ".yml"])
])
def test_output_file_format(file_format, extension, setup_test_result_dir_and_teardown): # 테스트 환경 tox.ini
# Given
test_result_dir = setup_test_result_dir_and_teardown

# When
success, _ = run_command("fosslight_bin", "-p", ".", "-o", test_result_dir, "-f", file_format)
files_in_result = os.listdir(test_result_dir)

# Then
if isinstance(extension, list):
# 생성된 파일 명칭을 리스트로 갖고오는 로직
matching_files = [f for f in files_in_result if any(f.endswith(ext) for ext in extension)]
else:
matching_files = [f for f in files_in_result if f.endswith(extension)]

assert success is True, f"Test Output File : {file_format} files not properly created (not create)"
assert len(matching_files) > 0, f"Test Output File : {file_format} files not properly created (length)"
77 changes: 77 additions & 0 deletions tests/initial_tox_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0

import os
import subprocess
import shutil
import pytest

TEST_RESULT_DIR = "test_result" # 테스트 디렉토리 명


@pytest.fixture(scope="function", autouse=True) # 테스트 디렉토리 생성/삭제 로직
def setup_test_result_dir_and_teardown():
print("==============setup==============")
os.makedirs(TEST_RESULT_DIR, exist_ok=True)

yield

print("==============tearDown==============")
shutil.rmtree(TEST_RESULT_DIR)


def run_command(*args): # 리눅스 명령어 실행
result = subprocess.run(args, capture_output=True, text=True)
success = result.returncode == 0
output = result.stdout if success else result.stderr
return success, output


def test_test_run_environment(): # 테스트 환경 tox.ini
# When
csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "csv")
exclude_success, _ = run_command(
"fosslight_bin",
"-p", ".",
"-e", "test", "commons-logging-1.2.jar",
"-o", "test_result/exclude_result.csv",
"-f", "csv"
)

files_in_result = run_command("ls", "test_result")[1].split()
txt_success, txt_output = run_command("find", "test_result", "-type", "f", "-name", "fosslight_log*.txt")

# Then
csv_files = [f for f in files_in_result if f.endswith('.csv')]
txt_files = txt_output.split()
assert csv_success is True, "Test Run Environment: CSV files not properly created (not create)"
assert len(csv_files) > 0, "Test Run Environment: CSV files not properly created (length)"
assert exclude_success is True, "Test Run Environment: Exclude feature fail"
assert len(txt_files) > 0, "Test Run Environment: txt files not properly created"


def test_release_environment(): # 릴리즈 환경 tox.ini
# When
help_success, _ = run_command("fosslight_bin", "-h")
csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.csv", "-f", "csv")
exclude_csv_success, _ = run_command(
"fosslight_bin",
"-p", ".",
"-e", "test", "commons-logging-1.2.jar",
"-o", "test_result/exclude_result.csv",
"-f", "csv"
)

json_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.json", "-f", "opossum")
files_in_result = run_command("ls", "test_result")[1].split()

# Then
required_files = ['result.csv', 'exclude_result.csv', 'result.json']
files_exist = all(f in files_in_result for f in required_files)
assert help_success is True, "Release Run Environment: help method not properly processing"
assert csv_success is True, "Release Run Environment: CSV files not properly created"
assert exclude_csv_success is True, "Release Run Environment: Exclude feature fail"
assert json_success is True, "Release Run Environment: json files not properly created"
assert files_exist is True, "Release Run Environment: Required files not properly created"
27 changes: 12 additions & 15 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,25 @@ ignore = E722, W503
filterwarnings = ignore::DeprecationWarning

[testenv:test_run]
deps =
-r{toxinidir}/requirements-dev.txt
changedir = tests
commands =
rm -rf test_result
fosslight_bin -p tests -o test_result -f csv
fosslight_bin -p tests -e test commons-logging-1.2.jar -o test_result/exclude_result.csv -f csv
ls test_result/
bash -c 'find test_result -type f -name "fosslight_report*.csv" | xargs cat'
bash -c 'find test_result -type f -name "fosslight_binary*.txt" | xargs cat'
pytest -n 4 initial_tox_test.py::test_test_run_environment
s-cu-bot marked this conversation as resolved.
Show resolved Hide resolved
s-cu-bot marked this conversation as resolved.
Show resolved Hide resolved

[testenv:release]
[testenv:format_test]
deps =
-r{toxinidir}/requirements-dev.txt
changedir = tests
commands =
pytest -n 4 file_format_test.py::test_output_file_format

[testenv:release]
deps =
-r{toxinidir}/requirements-dev.txt
commands =
fosslight_bin -h
fosslight_bin -p tests -o test_result/result.csv -f csv
fosslight_bin -p tests -e test commons-logging-1.2.jar -o test_result/exclude_result.csv -f csv
ls test_result/
cat test_result/result.csv
cat test_result/exclude_result.csv
fosslight_bin -p tests -o test_result/result.json -f opossum
pytest -n 4 tests/initial_tox_test.py::test_release_environment
pytest -v --flake8
pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks --hidden-import=pkg_resources.extern
{toxinidir}/dist/cli -p tests -o test_result_cli
; py.test --cov-report term-missing --cov={envsitepackagesdir}/fosslight_binary

Loading