Created
October 10, 2022 20:36
-
-
Save lenosi/12837a813d448ba28214b8ffc2a18763 to your computer and use it in GitHub Desktop.
Script to find potentially ignored JUnit tests by Maven(Surefire Plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import re | |
import argparse | |
from pathlib import Path | |
# Here's an example of usage | |
# python3 find_unused_tests.py ./my_java_project/tests | |
# TODO:Allowlist of ignored files/tests | |
# By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns: | |
# https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html | |
maven_default_file_include = [ | |
"**/Test*.java", # - includes all of its subdirectories and all Java filenames that start with "Test". | |
"**/*Test.java", # - includes all of its subdirectories and all Java filenames that end with "Test". | |
"**/*Tests.java", # - includes all of its subdirectories and all Java filenames that end with "Tests". | |
"**/*TestCase.java", # - includes all of its subdirectories and all Java filenames that end with "TestCase". | |
] | |
def main(inputs): | |
all_java_files = list(Path(inputs.path).rglob("**/*.java")) | |
tests_files = [] | |
for mvn in maven_default_file_include: | |
tests_files += list(Path(inputs.path).rglob(mvn)) | |
compared_list = list(set(all_java_files) - set(tests_files)) | |
pattern_annotation = re.compile(r"@Test") | |
pattern_test = re.compile(r"test[a-zA-Z]+\(\)") | |
next_line = False | |
for file in compared_list: | |
for i, line in enumerate(open(file)): | |
if next_line: | |
for match in re.finditer(pattern_test, line): | |
sys.stdout.write(f" {match.group()}\n") | |
next_line = False | |
else: | |
for match in re.finditer(pattern_annotation, line): | |
next_line = True | |
sys.stdout.write(f"{file} line {i+2}:") | |
def dir_path(string): | |
if os.path.isdir(string): | |
return string | |
else: | |
raise NotADirectoryError(string) | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("path", type=dir_path, help="Path to tests directory") | |
args = parser.parse_args() | |
return args | |
if __name__ == "__main__": | |
inputs = parse_args() | |
main(inputs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment