This project recommends unit tests for a piece of code or a short description. It summarizes each test, embeds that summary, and retrieves the closest tests by cosine similarity.
python -m pip install -r requirements.txtThe first run downloads Salesforce/codet5-base-multi-sum and sentence-transformers/multi-qa-MiniLM-L6-dot-v1. CUDA device 0 is used when CUDA is available. CPU is used when CUDA is not available. The summarizer is loaded with the tokenizer and seq2seq model directly, so it runs on both Transformers 4 and Transformers 5.
Optional extras:
requirements-dev.txtinstalls pytest.requirements-plot.txtinstalls the dependencies forplot.py.requirements-finetune.txtinstallssalesforce-codetfforfinetune.py. That package provides thecodetfmodule and targets older Python releases, so install it in a separate environment when it conflicts with the runtime packages.
Text similarity:
python main.py 1 "A test for a function that adds two numbers"Code similarity:
python main.py 2 "def add_numbers(a, b): return a + b"The command writes the five closest tests to results.txt. --top-k changes how many tests are returned:
python main.py 1 "A test for a function that adds two numbers" --top-k 10With no arguments, the program asks for the mode and the query.
Timing is printed in plain text and as one machine-readable line:
TIMING {"get_user_input_time": 0.0, "load_unit_tests_time": 0.1, "model_init_time": 1.2, "similarity_time": 0.05, "total_execution_time": 1.35}
test.py reads that line. Run it from the repository root:
python test.py- Load the summarizer and the embedding model.
- Read unit tests from
unittests/, including nested directories. The folder holds the original Python examples, unit tests copied from OpenTailcat, and one JUnit test from each of 1,010 repositories in the Methods2Test test split. Summaries and embeddings are cached inunittest_embeddings.json. Added, edited, and deleted tests update that cache. The FAISS index is rebuilt from the cache, so every vector maps to a file that still exists. - Embed the query. Code queries are summarized first. Text queries are embedded directly.
- Rank tests by cosine similarity. Embeddings are L2-normalized and searched with an inner-product index, which orders candidates by that same score. Results are written to
results.txt.
On those 1,010 Methods2Test methods, a word search over the test source placed the mapped test first more often than this index: Hit@1 0.63 against 0.33, and Hit@5 0.83 against 0.50.
The program reads and writes unittest_embeddings.json only. An older unittest_embeddings.pkl file is ignored, because loading a pickle can execute code stored in the file. Delete that file if it is still present.
python -m pip install -r requirements-dev.txt
python -m pytestThe tests cover index updates, deletions, cosine ranking, empty indexes, argument validation, and the benchmark parser. They do not download the models.
Suggestions, bug reports, and pull requests are welcome.
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
