PyTestAI-Generator
PyTestAI-Generator is a CLI tool that automatically generates pytest-compatible test cases for your Python files using the DeepSeek API.
It creates a ready-to-run test file (test_<filename>.py) in the same directory, ensuring proper imports and test coverage.The new version requires users to mark the functions they want to generate test cases for with the @include_in_test decorator.
Features
✔ Automated Test Generation – No need to write tests manually.
✔ AI-Powered – Uses the DeepSeek API to generate meaningful test cases.
✔ Ready-to-Run – Generates test files that can be executed directly with pytest.
✔ Customizable – Includes AI-generated comments for better understanding.
✔ Simple CLI Interface – Just run a command, and your tests are ready!
Installation
Prerequisites
🔹 Python 3.8 or higher
🔹 A valid DeepSeek API key
Install the package
pip install PyTestAI-Generator
Set the DeepSeek API Key
Set your DeepSeek API key as an environment variable:
✅ macOS / Linux
export DEEPSEEK_API_KEY="your_api_key_here"
✅ Windows (Command Prompt)
set DEEPSEEK_API_KEY="your_api_key_here"
✅ Windows (PowerShell)
$env:DEEPSEEK_API_KEY="your_api_key_here"
🛠 Usage
New Usage Workflow
Starting with this version, you must mark the functions or classes you want to generate test cases for with the @include_in_test decorator. After marking the relevant functions/classes, you can use the pytestai command to generate the tests.
-
Mark Functions/Classes with
@include_in_test
For every function or class you want to include in the test case generation, decorate it with@include_in_test. For example:from PyTestAI import include_in_test @include_in_test def add(a, b): return a + b
-
Generate the Test File
Once your functions are decorated, run thepytestaicommand followed by the path to your Python file:pytestai path/to/your_file.pyThis will generate a test file named
test_your_file.pyin the same directory.
Example
Given a Python file math_ops.py:
# math_ops.py
from PyTestAI import include_in_test
@include_in_test
def add(a, b):
return a + b
@include_in_test
def subtract(a, b):
return a - b
Run:
pytestai math_ops.py
This will generate test_math_ops.py:
# Here is a pytest-compatible test file for the `math_ops.py` module. This test file imports the `add` and `subtract` functions and includes test cases to verify their functionality.
# test_math_ops.py
# Import the functions to be tested
from math_ops import add, subtract
# Test cases for the add function
def test_add():
# Test addition of two positive numbers
assert add(2, 3) == 5, "Addition of 2 and 3 should be 5"
# Test addition of a positive and a negative number
assert add(-1, 1) == 0, "Addition of -1 and 1 should be 0"
# Test addition of two negative numbers
assert add(-5, -7) == -12, "Addition of -5 and -7 should be -12"
# Test addition with zero
assert add(0, 0) == 0, "Addition of 0 and 0 should be 0"
assert add(5, 0) == 5, "Addition of 5 and 0 should be 5"
# Test cases for the subtract function
def test_subtract():
# Test subtraction of two positive numbers
assert subtract(5, 3) == 2, "Subtraction of 5 and 3 should be 2"
# Test subtraction of a positive and a negative number
assert subtract(5, -3) == 8, "Subtraction of 5 and -3 should be 8"
# Test subtraction of two negative numbers
assert subtract(-5, -3) == -2, "Subtraction of -5 and -3 should be -2"
# Test subtraction with zero
assert subtract(0, 0) == 0, "Subtraction of 0 and 0 should be 0"
assert subtract(5, 0) == 5, "Subtraction of 5 and 0 should be 5"
assert subtract(0, 5) == -5, "Subtraction of 0 and 5 should be -5"
⚙ Configuration
Environment Variables
DEEPSEEK_API_KEY– Your DeepSeek API key (required for the tool to function).
🎯 Contributing
Contributions are welcome!
Steps to Contribute
- Fork the repository.
- Create a new branch (
feature-branch). - Make your changes & commit them (
git commit -m "Added new feature"). - Submit a pull request on GitHub.
💡 Feel free to open an issue if you encounter a bug or have a feature request!
License
This project is licensed under the MIT License. See the LICENSE file for details.
🛠 Support
If you have any issues or questions, feel free to open an issue on the
GitHub Repository.
🙌 Acknowledgments
- DeepSeek – For providing the AI-powered API used to generate test cases.
- pytest – For being an amazing Python testing framework.
👨💻 Author
- GitHub: Pawan Perera
- Email: 1pawanpinsara@gmail.com
Important Change:
In the new version, you must decorate each function or class you want to include in the test case generation with the @include_in_test decorator. If no decorator is applied, that function/class will be excluded from the generated test cases.
Metadata
Release files for PyTestAI-Generator 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pytestai_generator-2.0.1.tar.gz | 11.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytestai_generator-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.8 kB
Release files / pytestai_generator-2.0.1.tar.gz
| Download URL | pytestai_generator-2.0.1.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6a112527ca3fefcaf776190f2293dfa18184798785570ecc28cb46ef9744fd61
|
|
BLAKE2b-256 checksum How to use checksums |
7cdbe27d962bd075563aabfe6b3e4382f6b49406b64e0d11e5015a6367b3acf9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|
Release files / pytestai_generator-2.0.1-py3-none-any.whl
| Download URL | pytestai_generator-2.0.1-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
45715eabcfcc17a896e35212c3367762a8c55e9ebf63b94c22dc7b6261a63358
|
|
BLAKE2b-256 checksum How to use checksums |
930b522e6058cdd172f6d9bc0f985019a299c13514643490318bb9813d8c160d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|
Security for PyTestAI-Generator 2.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.