Project

tng

0.0
No release in over 3 years
TNG (Test Next Generation) is a Rails gem that automatically generates comprehensive test files by analyzing your Ruby code using static analysis and AI. It supports models, controllers, and services with intelligent test case generation.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 3.3
>= 6.0, < 9.0
~> 1.5
~> 0.8.0
~> 1.4.0
~> 0.9.91
~> 0.7
 Project Readme

TNG - AI-Powered Test Generation for Rails

TNG automatically generates comprehensive tests for your Rails applications using static code analysis and AI. It analyzes your code structure, authentication patterns, and configuration to generate high-quality, contextual tests that follow your project's conventions.

Owner: Binary Dreams LLC

Supported Ruby Versions and Platforms

Ruby Version Supported
3.1.x
3.2.x
3.3.x
3.4.x
Platform Architecture Supported
macOS Intel (x86_64)
macOS ARM (M1/M2/M3)
Linux Intel (x86_64)
Linux ARM (aarch64)

Installation

Add to your Gemfile:

gem 'tng'

Install:

bundle install

Quick Start

1. Initialize Configuration

Generate the configuration file:

rails generate tng:install

This creates config/initializers/tng.rb with auto-detected settings for your Rails application.

2. Configure API Key

Add your API key from https://app.tng.sh/ to your environment or configuration:

# .env
TNG_API_KEY=your_api_key_here

Or directly in config/initializers/tng.rb:

config.api_key = ENV["TNG_API_KEY"]

3. Configure Authentication Methods

⚠️ IMPORTANT: You must configure your authentication methods for TNG to generate accurate tests.

Edit config/initializers/tng.rb and uncomment the authentication_methods section:

config.authentication_methods = [
  {
    method: "authenticate_user!",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "devise"
  }
]

4. Generate Tests

Interactive Mode (Recommended):

bundle exec tng

Direct Mode:

bundle exec tng app/controllers/users_controller.rb index

Interactive UI Features

Main Menu

  • Generate Tests: Browse and select files to generate tests for
  • View Stats: See project statistics and test coverage
  • System Status: Check configuration and system health
  • About: View version and license information
  • Exit: Quit the application

Generate Tests View

  • Browse Files: Navigate through 20+ file types (controllers, models, services, jobs, helpers, mailers, GraphQL, etc.)
  • Search: Find files quickly by name
  • Filter: Filter by file type
  • Select Methods: Choose specific methods to generate tests for
  • Preview: Review generated tests before saving
  • Pagination: Navigate through large file lists

Post-Generation Options

  • View Generated Test: Open the test file in your editor
  • Run Test: Execute the test immediately
  • Copy Test Command: Copy the test command to clipboard
  • Generate Another: Continue testing other methods
  • Back to Menu: Return to main menu

Supported File Types

TNG can generate tests for 20+ Rails file types:

File Type Path Test Path
Controllers app/controllers/ spec/controllers/ or test/controllers/
Models app/models/ spec/models/ or test/models/
Services app/services/ spec/services/ or test/services/
Jobs app/jobs/ spec/jobs/ or test/jobs/
Mailers app/mailers/ spec/mailers/ or test/mailers/
Helpers app/helpers/ spec/helpers/ or test/helpers/
Channels app/channels/ spec/channels/ or test/channels/
Policies app/policies/ spec/policies/ or test/policies/
Decorators app/decorators/ spec/decorators/ or test/decorators/
Presenters app/presenters/ spec/presenters/ or test/presenters/
Serializers app/serializers/ spec/serializers/ or test/serializers/
Forms app/forms/ spec/forms/ or test/forms/
Queries app/queries/ spec/queries/ or test/queries/
Lib lib/ spec/lib/ or test/lib/
GraphQL Resolvers app/graphql/resolvers/ spec/graphql/resolvers/
GraphQL Types app/graphql/types/ spec/graphql/types/
GraphQL Mutations app/graphql/mutations/ spec/graphql/mutations/
GraphQL Loaders app/graphql/loaders/ spec/graphql/loaders/
GraphQL Schemas app/graphql/schemas/ spec/graphql/schemas/

Configuration Guide

Edit config/initializers/tng.rb to customize test generation.

Framework Detection

Test Frameworks:

Framework Supported
minitest
rspec

Configuration:

TESTING_FRAMEWORK = "minitest"  # Options: minitest, rspec

Minitest Configuration

Test Styles:

Style Supported Example
test_block test "does something" do ... end
spec it "does something" do ... end
unit def test_does_something ... end

Assertion Styles:

Style Supported Example
assert/refute assert_equal, refute_nil
assert/assert_not assert_equal, assert_not_nil
must/wont value.must_equal, value.wont_be_nil

Configuration:

TEST_STYLE = "test_block"          # Options: test_block, spec, unit
ASSERTION_STYLE = "assert/refute"  # Options: assert/refute, assert/assert_not, must/wont
SETUP_STYLE = true                 # Options: true, false
TEARDOWN_STYLE = false             # Options: true, false

RSpec Configuration

Configuration:

DESCRIBE_STYLE = true          # Options: true, false
CONTEXT_STYLE = "context"      # Options: context, describe
IT_STYLE = "it"                # Options: it, specify
BEFORE_STYLE = "before"        # Options: before, setup
AFTER_STYLE = "after"          # Options: after, teardown
LET_STYLE = true               # Options: true, false
SUBJECT_STYLE = true           # Options: true, false

Mock & Testing Libraries

Mock Libraries:

Library Framework Supported
minitest/mock Minitest
mocha Both
rspec-mocks RSpec
flexmock RSpec

HTTP Mock Libraries:

Library Supported
webmock
vcr
httparty

Factory Libraries:

Library Supported
factory_bot
factory_girl
fabrication
fabricator
fixtures
active_record

Configuration:

MOCK_LIBRARY = "minitest/mock"      # Options: minitest/mock, mocha, rspec-mocks, flexmock, nil
HTTP_MOCK_LIBRARY = "webmock"       # Options: webmock, vcr, httparty, nil
FACTORY_LIBRARY = "factory_bot"     # Options: factory_bot, factory_girl, fabrication, fabricator, fixtures, active_record

Authentication & Authorization

Authentication Libraries:

Library Supported
devise
clearance
sorcery
session-based
jwt
token-based
basic-auth
oauth
custom

Authorization Libraries:

Library Supported
cancancan
pundit
rolify

Configuration:

AUTHENTICATION_ENABLED = true          # Options: true, false
AUTHENTICATION_LIBRARY = "devise"      # Options: devise, clearance, sorcery, nil
AUTHORIZATION_LIBRARY = "pundit"       # Options: cancancan, pundit, rolify, nil

Configuring Authentication Methods

TNG requires you to specify your authentication methods for accurate test generation:

config.authentication_methods = [
  {
    method: "authenticate_user!",              # The method name in your code
    file_location: "app/controllers/application_controller.rb",  # Where it's defined
    auth_type: "devise"                        # Authentication type
  }
]

Required Fields:

  • method - The authentication method name (e.g., authenticate_user!, require_login)
  • file_location - Path to the file where the method is defined
  • auth_type - Type of authentication (see supported types below)

Supported auth_type Values:

  • session - Session-based authentication
  • devise - Devise gem
  • jwt - JSON Web Token
  • token_auth - Token-based authentication
  • basic_auth - HTTP Basic Authentication
  • oauth - OAuth authentication
  • headers - Custom header authentication
  • custom - Custom authentication logic

Examples:

Devise Authentication:

config.authentication_methods = [
  {
    method: "authenticate_user!",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "devise"
  }
]

Session-Based Authentication:

config.authentication_methods = [
  {
    method: "require_login",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "session"
  }
]

JWT Authentication:

config.authentication_methods = [
  {
    method: "authenticate_with_token",
    file_location: "app/controllers/api/base_controller.rb",
    auth_type: "jwt"
  }
]

Multiple Authentication Methods:

config.authentication_methods = [
  {
    method: "authenticate_user!",
    file_location: "app/controllers/application_controller.rb",
    auth_type: "session"
  },
  {
    method: "authenticate_api_user!",
    file_location: "app/controllers/api/base_controller.rb",
    auth_type: "token_auth"
  }
]

If You Don't Use Authentication:

config.authentication_enabled = false
config.authentication_methods = []

If your library or option isn't listed: TNG auto-detects most common configurations. Review and edit the generated config/initializers/tng.rb to match your specific setup.

Code Reading

READ_SOURCE_CODE = true   # Analyzes the file being tested
READ_TEST_CODE = true     # Reads existing tests to match your patterns

Usage

Interactive Mode

Launch the interactive UI:

bundle exec tng

Navigate through your application to select files and methods for test generation.

Direct Mode

Generate tests directly from the command line:

# Full path with method
bundle exec tng app/controllers/users_controller.rb index

# Shorthand
bundle exec tng f=users_controller.rb m=show

# Long form
bundle exec tng --file=app/models/user.rb --method=validate

# From subdirectories (auto-detection)
bundle exec tng users_controller.rb index

Direct Mode Features:

  • Automatic file type detection
  • Flexible file path resolution
  • Multiple syntax formats (f= or --file=)
  • Smart path finding

Debug Mode

Enable detailed logging:

# Interactive mode
DEBUG=1 bundle exec tng

# Direct mode
DEBUG=1 bundle exec tng app/controllers/users_controller.rb index

Help

bundle exec tng --help
bundle exec tng -h

How It Works

  1. Static Analysis: TNG uses Rust-based static analysis to parse your Ruby code and extract:

    • Method signatures and parameters
    • Controller routes and actions
    • Model associations and validations
    • Database relationships
    • Authentication patterns
    • Service object patterns
  2. Configuration Context: Reads your configuration to understand:

    • Testing framework preferences
    • Authentication/authorization setup
    • Factory and mock library choices
    • Existing test patterns
  3. AI Generation: Sends analyzed data to TNG API, which uses LLMs to generate:

    • Contextual test cases
    • Proper setup/teardown
    • Authentication mocks
    • Database fixtures
    • Edge case coverage
  4. Test Saving: Generates tests in the correct location (spec/ or test/) matching your framework conventions.

Troubleshooting

API Key Issues

Error: "Invalid API key"

  • Verify your API key from https://app.tng.sh/
  • Check environment variable: echo $TNG_API_KEY
  • Ensure no extra spaces in .env file

Error: "API key not found"

  • Set environment variable: export TNG_API_KEY=your_key
  • Or add to .env: TNG_API_KEY=your_key
  • Restart Rails server after changes

Configuration Issues

Error: "Invalid configuration"

  • Run: rails console then Tng.configuration
  • Verify all required settings are present
  • Check for typos in config/initializers/tng.rb

Poor Test Quality

  • Verify authentication methods are correctly configured
  • Check factory library setting matches your setup
  • Ensure read_source_code and read_test_code are enabled
  • Review testing framework settings

Authentication Configuration Issues

Tests don't include authentication

  • Verify authentication_enabled = true
  • Check authentication methods are correctly specified
  • Ensure file paths exist: ls app/controllers/application_controller.rb
  • Verify method names exist in specified files

Authentication tests fail

  • Check auth method names match your application
  • Verify file locations are correct
  • Ensure auth_type matches your authentication pattern

Generation Issues

No Tests Generated

  1. Verify file path is correct: ls app/controllers/users_controller.rb
  2. File contains valid Ruby code
  3. Configuration is valid
  4. API key is working
  5. Internet connection is available

Generated Tests Are Generic

  • Authentication not properly configured
  • Factory library setting doesn't match your setup
  • read_source_code or read_test_code disabled
  • File analysis failed (check DEBUG mode)

Platform Issues

Error: "Unsupported platform"

  • Verify you're on macOS or Linux
  • Check: uname -a
  • TNG supports macOS (Intel/ARM) and Linux (Intel/ARM)

Error: "Binary not found"

  • Ensure binaries are present: ls binaries/
  • Reinstall gem: gem uninstall tng && gem install tng

License

Copyright (c) 2025 Binary Dreams LLC. All rights reserved.

See LICENSE.md for details.