File size: 19,155 Bytes
d086025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | 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
+ }
+}
|