Skip to content

Commit 84200ac

Browse files
authored
Refactor existing tox test to pytest (#123)
Signed-off-by: s-cu-bot <boanboan114@gmail.com>
1 parent 6658d24 commit 84200ac

File tree

4 files changed

+144
-15
lines changed

4 files changed

+144
-15
lines changed

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ tox
33
pytest
44
pytest-cov
55
pytest-flake8
6+
pytest-xdist
67
flake8==3.9.2

tests/file_format_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2020 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
import os
7+
import subprocess
8+
import shutil
9+
import pytest
10+
import tempfile
11+
12+
13+
@pytest.fixture(scope="function", autouse=True)
14+
def setup_test_result_dir_and_teardown():
15+
# 각 테스트마다 임시 디렉토리 생성
16+
test_result_dir = tempfile.mkdtemp(dir=".") # 고유한 임시 디렉토리 생성
17+
print("==============setup: {test_result_dir}==============")
18+
19+
yield test_result_dir # 임시 디렉토리 경로를 테스트 함수로 넘김
20+
21+
print("==============tearDown==============")
22+
shutil.rmtree(test_result_dir)
23+
24+
25+
def run_command(*args): # 리눅스 명령어 실행
26+
result = subprocess.run(args, capture_output=True, text=True)
27+
success = result.returncode == 0
28+
output = result.stdout if success else result.stderr
29+
return success, output
30+
31+
32+
@pytest.mark.parametrize("file_format, extension", [
33+
("excel", ".xlsx"),
34+
("csv", ".csv"),
35+
("opossum", ".json"),
36+
("yaml", [".yaml", ".yml"])
37+
])
38+
def test_output_file_format(file_format, extension, setup_test_result_dir_and_teardown): # 테스트 환경 tox.ini
39+
# Given
40+
test_result_dir = setup_test_result_dir_and_teardown
41+
42+
# When
43+
success, _ = run_command("fosslight_bin", "-p", ".", "-o", test_result_dir, "-f", file_format)
44+
files_in_result = os.listdir(test_result_dir)
45+
46+
# Then
47+
if isinstance(extension, list):
48+
# 생성된 파일 명칭을 리스트로 갖고오는 로직
49+
matching_files = [f for f in files_in_result if any(f.endswith(ext) for ext in extension)]
50+
else:
51+
matching_files = [f for f in files_in_result if f.endswith(extension)]
52+
53+
assert success is True, f"Test Output File : {file_format} files not properly created (not create)"
54+
assert len(matching_files) > 0, f"Test Output File : {file_format} files not properly created (length)"

tests/initial_tox_test.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2020 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
import os
7+
import subprocess
8+
import shutil
9+
import pytest
10+
11+
TEST_RESULT_DIR = "test_result" # 테스트 디렉토리 명
12+
13+
14+
@pytest.fixture(scope="function", autouse=True) # 테스트 디렉토리 생성/삭제 로직
15+
def setup_test_result_dir_and_teardown():
16+
print("==============setup==============")
17+
os.makedirs(TEST_RESULT_DIR, exist_ok=True)
18+
19+
yield
20+
21+
print("==============tearDown==============")
22+
shutil.rmtree(TEST_RESULT_DIR)
23+
24+
25+
def run_command(*args): # 리눅스 명령어 실행
26+
result = subprocess.run(args, capture_output=True, text=True)
27+
success = result.returncode == 0
28+
output = result.stdout if success else result.stderr
29+
return success, output
30+
31+
32+
def test_test_run_environment(): # 테스트 환경 tox.ini
33+
# When
34+
csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "csv")
35+
exclude_success, _ = run_command(
36+
"fosslight_bin",
37+
"-p", ".",
38+
"-e", "test", "commons-logging-1.2.jar",
39+
"-o", "test_result/exclude_result.csv",
40+
"-f", "csv"
41+
)
42+
43+
files_in_result = run_command("ls", "test_result")[1].split()
44+
txt_success, txt_output = run_command("find", "test_result", "-type", "f", "-name", "fosslight_log*.txt")
45+
46+
# Then
47+
csv_files = [f for f in files_in_result if f.endswith('.csv')]
48+
txt_files = txt_output.split()
49+
assert csv_success is True, "Test Run Environment: CSV files not properly created (not create)"
50+
assert len(csv_files) > 0, "Test Run Environment: CSV files not properly created (length)"
51+
assert exclude_success is True, "Test Run Environment: Exclude feature fail"
52+
assert len(txt_files) > 0, "Test Run Environment: txt files not properly created"
53+
54+
55+
def test_release_environment(): # 릴리즈 환경 tox.ini
56+
# When
57+
help_success, _ = run_command("fosslight_bin", "-h")
58+
csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.csv", "-f", "csv")
59+
exclude_csv_success, _ = run_command(
60+
"fosslight_bin",
61+
"-p", ".",
62+
"-e", "test", "commons-logging-1.2.jar",
63+
"-o", "test_result/exclude_result.csv",
64+
"-f", "csv"
65+
)
66+
67+
json_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.json", "-f", "opossum")
68+
files_in_result = run_command("ls", "test_result")[1].split()
69+
70+
# Then
71+
required_files = ['result.csv', 'exclude_result.csv', 'result.json']
72+
files_exist = all(f in files_in_result for f in required_files)
73+
assert help_success is True, "Release Run Environment: help method not properly processing"
74+
assert csv_success is True, "Release Run Environment: CSV files not properly created"
75+
assert exclude_csv_success is True, "Release Run Environment: Exclude feature fail"
76+
assert json_success is True, "Release Run Environment: json files not properly created"
77+
assert files_exist is True, "Release Run Environment: Required files not properly created"

tox.ini

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,25 @@ ignore = E722, W503
2424
filterwarnings = ignore::DeprecationWarning
2525

2626
[testenv:test_run]
27+
deps =
28+
-r{toxinidir}/requirements-dev.txt
29+
changedir = tests
2730
commands =
28-
rm -rf test_result
29-
fosslight_bin -p tests -o test_result -f csv
30-
fosslight_bin -p tests -e test commons-logging-1.2.jar -o test_result/exclude_result.csv -f csv
31-
ls test_result/
32-
bash -c 'find test_result -type f -name "fosslight_report*.csv" | xargs cat'
33-
bash -c 'find test_result -type f -name "fosslight_binary*.txt" | xargs cat'
31+
pytest -n 4 initial_tox_test.py::test_test_run_environment
3432

35-
[testenv:release]
33+
[testenv:format_test]
3634
deps =
3735
-r{toxinidir}/requirements-dev.txt
36+
changedir = tests
37+
commands =
38+
pytest -n 4 file_format_test.py::test_output_file_format
3839

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

0 commit comments

Comments
 (0)