This repository contains a Python script (script.py) to automate the testing process for Java files. The automation ensures all Java files are tested individually by renaming their public class and references to Main, compiling, and running them with a single test file (MainTest.java). This setup is designed for projects where multiple Java files are tested independently against a single unit test file.
MainTest.javarefers to the file containing the unit tests for your Java files.Main.javarefers to the main code file after its public class has been renamed toMain.
-
Single Test File:
- Ensure
MainTest.javais the only test file in the directory, and it must be named exactlyMainTest.java. - This file should contain all the necessary JUnit tests.
- Ensure
-
Java File Requirements:
- Your repository can contain any number of Java files located anywhere within the directory (including subdirectories).
- All Java files (except
MainTest.java) are automatically processed and renamed toMain.
-
Self-Contained Files:
- Ideally,
Main.javaandMainTest.javashould be self-contained. Avoid importing additional classes from other files if possible. - If
MainTest.javaneeds to reference a helper class from your main code, declare it as apublic static classinsideMain.java. You can then access it inMainTest.javaviaMain.ClassName.
- Ideally,
-
Python (Version 3.6 or later)
- Download Python if you have not already.
- Ensure
pythonorpython3is accessible in your PATH.
-
Gradle (Version 7.0 or later)
- Download Gradle and follow the installation instructions.
- Make sure the
gradlecommand is accessible in your PATH.
A typical structure before running the script might look like:
.
├── MainTest.java # Your single unit test file (mandatory)
├── script.py # The Python script to automate testing
├── File1.java # Example Java file
├── File2.java # Another example Java file
└── Subdirectory/
├── File3.java
└── File4.java
-
Renaming Classes:
- The script finds all Java files in your directory (excluding
MainTest.javaand build-related directories). - It renames the public class in each file to
Mainand updates all references to the original class name.
- The script finds all Java files in your directory (excluding
-
Testing Each File:
- The script temporarily creates a Gradle project structure (
src/main/java/andsrc/test/java/) and places:- The renamed Java file into
src/main/java/Main.java - The test file (
MainTest.java) intosrc/test/java/MainTest.java
- The renamed Java file into
- It then runs Gradle to build and test the file.
- The script temporarily creates a Gradle project structure (
-
Code Coverage:
- The script is configured to generate a code coverage CSV report. However, due to the repeated renaming process, coverage results might not always be reliable for the entire codebase.
- For more in-depth coverage analysis, consider using an IDE such as IntelliJ.
-
Cleanup:
- After testing each file, the script removes the temporary Gradle project directories and any generated build artifacts. This ensures a clean environment before moving on to the next file.
- Clone/Download this repository or place
script.pyin your existing project directory. - Make sure your directory contains:
MainTest.java(with the correct name).- Any number of other Java files to be tested.
- Open a terminal or command prompt in the project directory.
- Run the script using one of the following:
# Option 1: Directly with Python python script.py
- The script will display messages indicating:
- Which Java files are being renamed.
- The result of each Gradle test run (passed, failed, skipped).
- Cleanup status after each file is processed.
After all tests complete, the script automatically cleans the generated directories (build, src, .gradle, etc.). Your original Java files remain untouched (except for the in-place renaming to Main), and the workspace returns to a clean state.