Conghao97's picture tcftrees's picture
ProgramGen release
d086025
Raw History Blame Contribute Delete
19.2 kB
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,1 @@
+"""Standard init file."""
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,25 @@
+"""Implement fixtures and hooks for pytest session."""
+
+import json
+import pathlib
+import pytest
+
+# pylint: disable=C0103,W0602
+full_test_data = {}
+
+
+@pytest.hookimpl()
+def pytest_sessionstart():
+ """Read and collect test data as part of the global test data dictionary."""
+ test_data_files = pathlib.Path("./tests/test_data").glob("*.json")
+ for json_data_file in test_data_files:
+ with open(json_data_file, "r", encoding="utf-8") as data_file:
+ current_test_data = json.load(data_file)
+ global full_test_data
+ full_test_data[json_data_file.stem] = current_test_data
+
+
+@pytest.fixture
+def test_data():
+ """Return full_test_data."""
+ return full_test_data
diff --git a/tests/test_data/__init__.py b/tests/test_data/__init__.py
new file mode 100644
--- /dev/null
+++ b/tests/test_data/__init__.py
@@ -0,0 +1,1 @@
+"""Standard init file."""
diff --git a/tests/test_data/sample_file.py b/tests/test_data/sample_file.py
new file mode 100644
--- /dev/null
+++ b/tests/test_data/sample_file.py
@@ -0,0 +1,23 @@
+"""Use module is used for testing purposes only, do not modify."""
+# pylint: disable=C0116, E0602, W0612
+
+
+def some_function(arg1, arg2, arg3, arg4): # noqa
+ print("hello world!")
+ int1 = arg1 * arg3 / 5 - arg4
+ int2 = arg4 ** arg3
+ my_list = [1, 2, 3.6, 4, 5.4, "some string"]
+ my_dictionary = {
+ "first": arg1,
+ "second": arg2,
+ "third": arg3,
+ "fourth": arg4,
+ }
+ my_tuple = ("hello", "world")
+ result = some_other_function(arg2).process_somehow()
+ if not arg3 > arg2 and arg1 < arg4:
+ return arg2 >> 5
+ while my_list[4] > my_dictionary["first"]:
+ my_variable = 54
+ my_variable += arg1 / arg4
+ return my_dictionary["first"] + "something here"
diff --git a/tests/test_line.py b/tests/test_line.py
new file mode 100644
--- /dev/null
+++ b/tests/test_line.py
@@ -0,0 +1,148 @@
+"""Test the line module and Line class."""
+
+import pytest
+
+from afluent import line
+
+
+def test_globals():
+ """Check that global constants are of specific values."""
+ assert line.TARAN == "tarantula"
+ assert line.OCHIAI == "ochiai"
+ assert line.OCHIAI2 == "ochiai2"
+ assert line.DSTAR == "dstar"
+ assert line.RANDOM == "random"
+ assert line.CYCLOMATIC == "cyclomatic"
+ assert line.LOGICAL == "logical"
+ assert line.ENHANCED == "enhanced"
+
+
+@pytest.mark.parametrize(
+ "failed_cover,passed_cover,total_passed,total_failed,expected_score",
+ [
+ (1, 0, 1, 1, 1),
+ (0, 1, 1, 1, 0),
+ (3, 1, 6, 4, 0.8182),
+ (0, 3, 5, 0, 0),
+ (4, 0, 0, 9, 1),
+ ],
+)
+def test_tarantula(
+ failed_cover, passed_cover, total_passed, total_failed, expected_score
+):
+ """Check that the tarantula formula is correct."""
+ assert (
+ line.Line.tarantula(failed_cover, passed_cover, total_passed, total_failed)
+ == expected_score
+ )
+
+
+@pytest.mark.parametrize(
+ "failed_cover,passed_cover,total_failed,expected_score",
+ [
+ (1, 0, 1, 1),
+ (3, 2, 6, 0.5477),
+ (0, 1, 6, 0),
+ ],
+)
+def test_ohiai(failed_cover, passed_cover, total_failed, expected_score):
+ """Check that the ohiai formula is correct."""
+ assert line.Line.ochiai(failed_cover, passed_cover, total_failed) == expected_score
+
+
+@pytest.mark.parametrize(
+ "failed_cover,passed_cover,total_failed,power,expected_score",
+ [
+ (1, 0, 2, 3, 1),
+ (3, 2, 6, 3, 5.4),
+ (0, 1, 6, 3, 0),
+ (6, 0, 6, 3, float("inf")),
+ ],
+)
+def test_dstar(failed_cover, passed_cover, total_failed, power, expected_score):
+ """Check that the test_dstar formula is correct."""
+ assert (
+ line.Line.dstar(failed_cover, passed_cover, total_failed, power=power)
+ == expected_score
+ )
+
+
+@pytest.mark.parametrize(
+ "failed_cover,passed_cover,total_passed,total_failed,expected_score",
+ [
+ (3, 0, 0, 6, 1),
+ (0, 0, 7, 0, 0),
+ (3, 3, 3, 3, 0),
+ (0, 0, 5, 5, 0),
+ (3, 2, 7, 6, 0.366),
+ ],
+)
+def test_ochiai2(
+ failed_cover, passed_cover, total_passed, total_failed, expected_score
+):
+ """Check that the ochiai2 formula is correct."""
+ assert (
+ line.Line.ochiai2(failed_cover, passed_cover, total_passed, total_failed)
+ == expected_score
+ )
+
+
+def test_line_create():
+ """Check that line object can be instantiated correctly."""
+ test_line = line.Line("sample/path/to/file.py", 14)
+ assert test_line.path == "sample/path/to/file.py"
+ assert test_line.number == 14
+ assert not (test_line.passed_by or test_line.failed_by or test_line.skipped_by)
+ assert list(test_line.sus_scores.values()) == [-1.0, -1.0, -1.0, -1.0]
+ assert list(test_line.tiebreakers.values()) == [0, 0, 0, 0]
+
+
+def test_line_sus_all():
+ """Check that all suspiciousness scores can be calculated."""
+ test_line = line.Line("sample/path/to/file.py", 14)
+ test_line.passed_by = ["test1", "test2"]
+ test_line.failed_by = ["test3", "test4", "test5"]
+ for sus_value in test_line.sus_scores.values():
+ assert sus_value == -1.0
+ test_line.sus_all(4, 6)
+ assert test_line.sus_scores["tarantula"] == 0.5
+ assert test_line.sus_scores["ochiai"] == 0.5477
+ assert test_line.sus_scores["dstar"] == 5.4
+
+
+def test_line_sus_unknown():
+ """Check that an error is thrown when an unknown method is passed."""
+ test_line = line.Line("sample/path/to/file.py", 14)
+ test_line.passed_by = ["test1", "test2"]
+ test_line.failed_by = ["test3", "test4", "test5"]
+ with pytest.raises(Exception):
+ test_line.sus("random", 5, 5)
+
+
+def test_as_csv():
+ """Check that a line is correctly converted to csv format."""
+ test_line = line.Line("sample/path/to/file.py", 14)
+ assert test_line.as_csv() == ["sample/path/to/file.py", 14, -1, -1, -1, -1]
+
+
+def test_sus_text():
+ """Check that the correct tuple is returned."""
+ test_line = line.Line("sample/path/to/file.py", 14)
+ test_line.sus_scores = {
+ "tarantula": 1.0,
+ "ochiai": 1.0,
+ "dstar": 15.0,
+ }
+ assert test_line.sus_text(["tarantula"]) == ("sample/path/to/file.py", 14, [1.0])
+ assert test_line.sus_text(["ochiai"]) == ("sample/path/to/file.py", 14, [1.0])
+ assert test_line.sus_text(["dstar"]) == ("sample/path/to/file.py", 14, [15.0])
+ assert test_line.sus_text(["dstar", "tarantula"]) == (
+ "sample/path/to/file.py",
+ 14,
+ [15.0, 1.0],
+ )
+
+
+# def test_something():
+# """Purposefully fail to check report."""
+# assert False
diff --git a/tests/test_proj_file.py b/tests/test_proj_file.py
new file mode 100644
--- /dev/null
+++ b/tests/test_proj_file.py
@@ -0,0 +1,139 @@
+"""Test the proj_file module and ProjFile class."""
+
+import pytest
+from afluent import line, proj_file
+
+
+def test_projfile_init():
+ """Test that a projFile object can be created."""
+ test_projfile = proj_file.ProjFile("samplename.py")
+ assert test_projfile.name == "samplename.py"
+ assert not test_projfile.lines
+
+
+def test_update_file_new_key():
+ """Check that a line is created and updated in a new file."""
+ test_projfile = proj_file.ProjFile("samplename.py")
+ covered_lines = [3, 6, 7, 9, 13]
+ test_projfile.update_file(covered_lines, "passed", "sample_testcase")
+ for line_num in covered_lines:
+ assert line_num in test_projfile.lines
+ assert isinstance(test_projfile.lines[line_num], line.Line)
+ assert test_projfile.lines[line_num].passed_by == ["sample_testcase"]
+
+
+def test_update_file_existing_key_passed():
+ """Check that a line is created and updated in a new file."""
+ test_projfile = proj_file.ProjFile("samplename.py")
+ test_projfile.lines[5] = line.Line("samplename.py", 5)
+ test_projfile.lines[5].passed_by = [
+ "sample_testcase2",
+ "sample_testcase3",
+ "sample_testcase4",
+ ]
+ covered_lines = [5, 6]
+ test_projfile.update_file(covered_lines, "passed", "sample_testcase")
+ assert test_projfile.lines[5].passed_by == [
+ "sample_testcase2",
+ "sample_testcase3",
+ "sample_testcase4",
+ "sample_testcase",
+ ]
+
+
+def test_update_file_existing_key_failed():
+ """Check that a line is created and updated in a new file."""
+ test_projfile = proj_file.ProjFile("samplename.py")
+ test_projfile.lines[5] = line.Line("samplename.py", 5)
+ test_projfile.lines[5].failed_by = [
+ "sample_testcase2",
+ "sample_testcase3",
+ "sample_testcase4",
+ ]
+ covered_lines = [5, 6]
+ test_projfile.update_file(covered_lines, "failed", "sample_testcase")
+ assert test_projfile.lines[5].failed_by == [
+ "sample_testcase2",
+ "sample_testcase3",
+ "sample_testcase4",
+ "sample_testcase",
+ ]
+
+
+def test_update_file_existing_key_skipped():
+ """Check that a line is created and updated in a new file."""
+ test_projfile = proj_file.ProjFile("samplename.py")
+ test_projfile.lines[5] = line.Line("samplename.py", 5)
+ test_projfile.lines[5].skipped_by = [
+ "sample_testcase2",
+ "sample_testcase3",
+ "sample_testcase4",
+ ]
+ covered_lines = [5, 6]
+ test_projfile.update_file(covered_lines, "skipped", "sample_testcase")
+ assert test_projfile.lines[5].skipped_by == [
+ "sample_testcase2",
+ "sample_testcase3",
+ "sample_testcase4",
+ "sample_testcase",
+ ]
+
+
+def test_update_file_unknown_result():
+ """Check that a line is created and updated in a new file."""
+ test_projfile = proj_file.ProjFile("samplename.py")
+ covered_lines = [5, 6]
+ with pytest.raises(Exception):
+ test_projfile.update_file(covered_lines, "random", "sample_testcase")
+
+
+def test_get_datasets():
+ """Check that all tiebreaker datasets can be populated. Regardless of accuracy."""
+ test_projfile = proj_file.ProjFile("./tests/test_data/sample_file.py")
+ assert not test_projfile.cyclomatic_complexity_data
+ assert not test_projfile.logical_tiebreak_data
+ assert not test_projfile.enhanced_tiebreak_data
+ test_projfile.get_cyclomatic_tiebreaker_dataset()
+ test_projfile.get_logical_tiebreaker_dataset()
+ test_projfile.get_enhanced_tiebreaker_dataset()
+ assert test_projfile.cyclomatic_complexity_data
+ assert test_projfile.logical_tiebreak_data
+ assert test_projfile.enhanced_tiebreak_data
+ covered_lines = [6, 7, 8, 10]
+ test_projfile.update_file(covered_lines, "failed", "sample_testcase")
+ assert test_projfile.lines[6].tiebreakers == {
+ "cyclomatic": 4,
+ "logical": 0.0,
+ "enhanced": 3.0,
+ "random": 0,
+ }
+ assert test_projfile.lines[7].tiebreakers == {
+ "cyclomatic": 4,
+ "logical": 3,
+ "enhanced": 4.5,
+ "random": 0.0,
+ }
+ assert test_projfile.lines[8].tiebreakers == {
+ "cyclomatic": 4,
+ "logical": 1,
+ "enhanced": 3.0,
+ "random": 0.0,
+ }
+ assert test_projfile.lines[10].tiebreakers == {
+ "cyclomatic": 4,
+ "logical": 0.0,
+ "enhanced": 5,
+ "random": 0.0,
+ }
+
+
+# def test_projfile_as_dict():
+# """Check that as_dict() return a correct dictionary."""
+# file_name = "samplename.py"
+# test_projfile = proj_file.ProjFile(file_name)
+# test_projfile.update_file([5], "passed", "sample_test")
+# output_dict = test_projfile.as_dict()
+# expected_line = line.Line(file_name, 5)
+# expected_line.passed_by = ["sample_test"]
+# expected_dict = {"5": expected_line.as_dict()}
+# assert output_dict == expected_dict
diff --git a/tests/test_spectrum_parser.py b/tests/test_spectrum_parser.py
new file mode 100644
--- /dev/null
+++ b/tests/test_spectrum_parser.py
@@ -0,0 +1,148 @@
+"""Include test cases on spectrum_parser module."""
+import pytest
+from afluent import spectrum_parser
+
+
+def test_spectrum_init(test_data):
+ """Check that spectrum initialization reassembles config and calculates scores."""
+ input_config_dict = test_data["test_spectrum_init"]["input_config"]
+ expected_output_dict = test_data["test_spectrum_init"]["output_reassembled"]
+ spectrum_object = spectrum_parser.Spectrum(input_config_dict)
+ assert spectrum_object.totals == expected_output_dict
+
+
+def test_spectrum_init_empty_config():
+ """Check that nothing will happen when empty config is passed to
+ Spectrum."""
+ spectrum_object = spectrum_parser.Spectrum({})
+ assert not spectrum_object.reassembled_data
+ assert spectrum_object.totals == {"passed": 0, "failed": 0, "skipped": 0}
+
+
+def test_spectrum_generate_report_throws_error(test_data):
+ """Check that error is thrown when unknown method is used."""
+ config_item = test_data["test_spectrum_init"]["input_config"]
+ spectrum_object = spectrum_parser.Spectrum(config_item)
+ with pytest.raises(Exception):
+ spectrum_object.generate_report("random")
+
+
+def test_spectrum_init_eval_mode():
+ """Check that all tiebreaker data is generated in eval mode."""
+ config = {
+ "test1": {
+ "coverage": {"tests/test_data/sample_file.py": [1, 2, 3, 4]},
+ "result": "passed",
+ }
+ }
+ spectrum_object = spectrum_parser.Spectrum(config, eval_mode=True)
+ assert spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].cyclomatic_complexity_data
+ assert spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].logical_tiebreak_data
+ assert spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].enhanced_tiebreak_data
+
+
+def test_spectrum_init_enhanced_tiebreaker():
+ """Check that only the data for the enhanced tiebreaker is calculated for
+ that mode."""
+ config = {
+ "test1": {
+ "coverage": {"tests/test_data/sample_file.py": [1, 2, 3, 4]},
+ "result": "passed",
+ }
+ }
+ spectrum_object = spectrum_parser.Spectrum(config, tiebreaker="enhanced")
+ assert not spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].cyclomatic_complexity_data
+ assert not spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].logical_tiebreak_data
+ assert spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].enhanced_tiebreak_data
+
+
+def test_spectrum_init_logical_tiebreaker():
+ """Check that only the data for the logical tiebreaker is calculated for
+ that mode."""
+ config = {
+ "test1": {
+ "coverage": {"tests/test_data/sample_file.py": [1, 2, 3, 4]},
+ "result": "passed",
+ }
+ }
+ spectrum_object = spectrum_parser.Spectrum(config, tiebreaker="logical")
+ assert not spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].cyclomatic_complexity_data
+ assert spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].logical_tiebreak_data
+ assert not spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].enhanced_tiebreak_data
+
+
+def test_spectrum_init_cyclomatic_tiebreaker():
+ """Check that only the data for the cyclomatic tiebreaker is calculated for
+ that mode."""
+ config = {
+ "test1": {
+ "coverage": {"tests/test_data/sample_file.py": [1, 2, 3, 4]},
+ "result": "passed",
+ }
+ }
+ spectrum_object = spectrum_parser.Spectrum(config, tiebreaker="cyclomatic")
+ assert spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].cyclomatic_complexity_data
+ assert not spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].logical_tiebreak_data
+ assert not spectrum_object.reassembled_data[
+ "tests/test_data/sample_file.py"
+ ].enhanced_tiebreak_data
+
+
+def test_spectrum_print_report_throws_error():
+ """Check that an error is thrown when an unknown report is requested."""
+ config = {
+ "test1": {
+ "coverage": {"tests/test_data/sample_file.py": [1, 2, 3, 4]},
+ "result": "passed",
+ }
+ }
+ spectrum_object = spectrum_parser.Spectrum(config, tiebreaker="cyclomatic")
+ with pytest.raises(Exception):
+ spectrum_object.print_report(["something"], items_num=20)
+
+
+@pytest.mark.parametrize(
+ "method,sus_score,rank,out_of,formatting_func",
+ [
+ ("tarantula", 0, 1, 1, spectrum_parser.PALETTE["safe"]),
+ ("tarantula", 0.5, 1, 20, spectrum_parser.PALETTE["risky"]),
+ ("tarantula", 0.5, 16, 20, spectrum_parser.PALETTE["mild"]),
+ ("tarantula", 1, 8, 20, spectrum_parser.PALETTE["severe"]),
+ ("ochiai", 0, 1, 1, spectrum_parser.PALETTE["safe"]),
+ ("ochiai", 0.5, 1, 20, spectrum_parser.PALETTE["risky"]),
+ ("ochiai", 0.5, 16, 20, spectrum_parser.PALETTE["mild"]),
+ ("ochiai", 1, 8, 20, spectrum_parser.PALETTE["severe"]),
+ ("dstar", 0, 1, 1, spectrum_parser.PALETTE["safe"]),
+ ("dstar", 0.5, 1, 20, spectrum_parser.PALETTE["risky"]),
+ ("dstar", 0.5, 16, 20, spectrum_parser.PALETTE["mild"]),
+ ("dstar", float("inf"), 8, 20, spectrum_parser.PALETTE["severe"]),
+ ],
+)
+def test_calculate_severity(method, sus_score, rank, out_of, formatting_func):
+ """Check that severity calculating returns the correct formatting function."""
+ assert (
+ spectrum_parser.Spectrum.calculate_severity(method, sus_score, rank, out_of)
+ == formatting_func
+ )
diff --git a/tests/test_data/test_spectrum_init.json b/tests/test_data/test_spectrum_init.json
new file mode 100644
--- /dev/null
+++ b/tests/test_data/test_spectrum_init.json
@@ -0,0 +1,34 @@
+{
+ "input_config": {
+ "test1": {
+ "coverage": {
+ "file1.py": [1, 2, 3, 4],
+ "file2.py": [1, 2, 3, 4, 5, 6]
+ },
+ "result": "passed"
+ },
+ "test2": {
+ "coverage": {
+ "file1.py": [3, 4, 5, 6],
+ "file2.py": [1, 2, 3, 6, 7, 8, 9]
+ },
+ "result": "passed"
+ },
+ "test3": {
+ "coverage": {
+ "file1.py": [5, 6, 7],
+ "file2.py": [9, 3, 5]
+ },
+ "result": "failed"
+ },
+ "test4": {
+ "coverage": {},
+ "result": "skipped"
+ }
+ },
+ "output_reassembled": {
+ "passed": 2,
+ "failed": 1,
+ "skipped": 1
+ }
+}