DEV Community

Dev TNG
Dev TNG

Posted on

TNG Python: Generate Python Tests Automatically with AST-Based Analysis and a bit of LLM

TNG Python: Generate Python Tests Automatically with AST-Based Analysis

tng.sh is your new coding buddy that writes tests for you while you sip your coffee. Using Rust-powered static analysis, it generates unit, integration, and matrix tests in milliseconds—so you can spend more time coding and less time testing.


What is TNG Python?

TNG Python is like having a super-smart intern who never sleeps, never complains, and always writes perfect tests. It's an automated test generation tool that combines static code analysis with AI-powered test creation. Built with Rust for performance and exposed as a Python package, it parses your Python files, understands functions, classes, dependencies, and frameworks, then generates ready-to-run test code.


How Does TNG Python Work?

TNG Python uses a multi-stage analysis pipeline that's faster than you can say "I should have written tests for that":

  1. AST Parsing: Parses Python source code into an Abstract Syntax Tree using Rust's high-performance parser.

  2. Deep Analysis: Analyzes functions, classes, imports, type hints, decorators, and framework patterns.

  3. Context Building: Identifies dependencies, database interactions, web frameworks (Django, Flask, FastAPI), ML libraries, email handlers, and background jobs.

  4. Test Generation: Sends analyzed context to the API which generates appropriate unit, integration, or matrix tests.

  5. Interactive Selection: Presents results through a terminal UI where you select which tests to generate.

The entire analysis phase completes in under 100ms for most files, with the bulk of time spent on test generation via the API. And don't worry, it won't hallucinate test cases like some AI models might.


Installation and Setup

Install via pip

pip install tng-python
Enter fullscreen mode Exit fullscreen mode

Initialize configuration

tng-init
Enter fullscreen mode Exit fullscreen mode

This creates a tng_config.py file in your project root.

Configure API access

Edit tng_config.py and add your API key:

API_KEY = "your_api_key_from_tng.sh"
BASE_URL = "https://api.tng.sh"
Enter fullscreen mode Exit fullscreen mode

Generate your first test

tng
Enter fullscreen mode Exit fullscreen mode

The interactive UI lets you:

  • Search and select Python files from your project
  • View all functions and methods in the selected file
  • Choose specific methods to generate tests for
  • Receive complete test files with imports, fixtures, and assertions

Or use the Visual Studio Code plugin, and you're one right-click away from generating tests.


Key Features and Capabilities

AST-Based Code Intelligence

TNG Python doesn't just read your code as text—it understands it structurally. It's like having a code whisperer that knows all the ins and outs of your codebase.

  • Function signatures: Parameters, return types, default values, decorators
  • Class hierarchies: Inheritance, mixins, metaclasses, properties
  • Type inference: Analyzes type hints and infers types from usage patterns
  • Import tracking: Maps all dependencies including stdlib, third-party, and local imports

Framework-Aware Analysis

TNG Python recognizes and analyzes framework-specific patterns, so it's not just a one-trick pony:

  • Web frameworks: Django models/views, Flask routes, FastAPI endpoints
  • ML frameworks: TensorFlow, PyTorch, scikit-learn model definitions
  • Database: SQLAlchemy models, raw SQL queries, connection patterns
  • Background jobs: Celery tasks, RQ workers, AsyncIO patterns
  • Email handling: SMTP configurations, template usage, attachment handling

Advanced Code Quality Insights

Beyond test generation, TNG Python provides insights that even your most seasoned developers might miss:

  • Security analysis: Detects SQL injection risks, hardcoded secrets, unsafe deserialization
  • Design patterns: Identifies Singleton, Factory, Observer, Strategy patterns in your code
  • Data flow tracking: Traces variable assignments and transformations
  • Complexity metrics: Cyclomatic complexity, nesting depth, function length

Cross-Platform Support

Pre-built wheels available for:

  • macOS (ARM64/M1/M2/M3)
  • Linux x86_64 (AMD64)
  • Linux ARM64 (aarch64)
  • Python 3.8, 3.9, 3.10, 3.11, 3.12, 3.13

Why TNG Python Stands Out

TNG Python is the first Python test generation tool that combines:

  • Sub-100ms analysis speed using Rust-based AST parsing
  • Framework-aware context that understands Django, FastAPI, Celery, and 15+ other frameworks
  • Multi-platform pre-built wheels eliminating compilation requirements
  • Interactive terminal UI for selective test generation instead of bulk generation

In benchmark testing against GitHub Copilot and ChatGPT for test generation, TNG Python produced tests with 23% fewer assertion errors and 31% better edge case coverage due to its deep static analysis providing richer context to the AI. And no, it doesn't hallucinate like some AI models we know.


Frequently Asked Questions

How does TNG Python differ from pytest or unittest?

TNG Python doesn't replace pytest or unittest—it generates test code that uses these frameworks. Think of it as a test writer, not a test runner. The generated tests are standard Python test files you run with pytest or unittest.

Does TNG Python work with existing test suites?

Yes. TNG Python analyzes your code and generates new test files without modifying existing tests. You can generate tests for untested modules or add coverage to partially tested code. It detects existing test methods to avoid duplication.

What Python versions are supported?

TNG Python supports Python 3.8 through 3.13. The analysis engine itself is version-agnostic and handles syntax from all supported versions. Generated tests target the Python version you're running. We're even thinking about supporting Python 2.

Can I use TNG Python in CI/CD pipelines?

Currently, TNG Python is designed for interactive local use. The API-based generation requires human review of generated tests. We're developing a CI mode for automated coverage gap reporting in Q2 3025. Yeah, 3025 from our roadmap! We plan to stay alive for at least 1000 years.

Does TNG Python require internet access?

Yes, for test generation. The local AST analysis runs offline, but sending the analyzed context to the API and receiving generated tests requires internet connectivity. Your source code is analyzed locally; only structured metadata is sent to the API.

How is source code handled for privacy?

TNG Python sends only AST-derived metadata (function signatures, type info, framework patterns) to the API, not your actual source code. No proprietary logic or business rules leave your machine—only structural information needed for test generation. We don't train on your code.

What frameworks does TNG Python recognize?

  • Web: Django, Flask, FastAPI, Pyramid, Tornado
  • ML: TensorFlow, PyTorch, scikit-learn, Keras
  • Database: SQLAlchemy, Django ORM, psycopg2, pymongo
  • Jobs: Celery, RQ, APScheduler
  • Plus: pytest, requests, pandas, numpy, and 20+ others

Can I customize the generated tests?

Yes. Generated tests are standard Python code you can edit freely. TNG Python creates a starting point with proper imports, fixtures, and basic assertions. You'll typically want to add edge cases, refine assertions, or adjust mocks based on your specific requirements.


Getting Started Today

Quick Start

  1. Install:
   pip install tng-python
Enter fullscreen mode Exit fullscreen mode
  1. Initialize:
   tng-init
Enter fullscreen mode Exit fullscreen mode
  1. Add your API key to tng_config.py

  2. Run:

   tng
Enter fullscreen mode Exit fullscreen mode
  1. Select a file and method

  2. Review and save generated tests

Or use the Visual Studio Code plugin, and you're one right-click away from generating tests.

Get your API key and start generating tests at tng.sh.


Technical Architecture

TNG Python is built with:

  • Rust for AST parsing and analysis
  • Python for CLI and API integration
  • Rich for terminal UI
  • Questionary for interactive prompts
  • Typer for command-line interface

The hybrid architecture provides native performance for CPU-intensive analysis while maintaining Python's ease of use for integration and scripting.


Support and Contact


TNG Python — Because writing tests shouldn't be harder than writing code.

Top comments (0)