Skip to content

Automated Testing

Dick Davis edited this page Jan 21, 2026 · 1 revision

Automated Testing

TokenAuthority uses RSpec for automated testing. The test suite includes unit tests for models and service objects, as well as request specs for controller endpoints.

Running Tests

Full Test Suite

Run the complete test suite:

bundle exec rspec

Single File

Run tests in a specific file:

bundle exec rspec spec/models/token_authority/client_spec.rb

Specific Test

Run a specific test by line number:

bundle exec rspec spec/models/token_authority/client_spec.rb:42

By Tag

Run tests with a specific tag:

bundle exec rspec --tag slow:factory

Parallel Execution

For faster execution on multi-core machines, you can use parallel_tests (if configured):

bundle exec parallel_rspec spec/

Test Organization

Tests are organized by type in the spec/ directory:

Directory Description
spec/models/ Unit tests for models and service objects
spec/requests/ Request specs for controller endpoints
spec/factories/ Factory definitions for test data
spec/dummy/ Dummy Rails application for integration testing

Performance Profiling

TokenAuthority includes test-prof for analyzing test suite performance. Use these commands to identify bottlenecks and optimization opportunities.

Execution Time by Spec Type

Measure how long each type of spec (models, requests, etc.) takes to run:

TAG_PROF=type bundle exec rspec

This helps identify which spec types are slowest and may need optimization.

Visual Performance Chart

Generate an HTML chart showing execution time by spec type and specific events:

TAG_PROF=type TAG_PROF_FORMAT=html TAG_PROF_EVENT=sql.active_record,factory.create bundle exec rspec

This creates a visual breakdown of where time is spent, combining spec type with database and factory activity.

Database Interaction Time

Measure time spent on database queries:

EVENT_PROF='sql.active_record' bundle exec rspec

High database time may indicate:

  • Missing database indexes
  • N+1 queries in application code
  • Excessive test data setup

Factory Execution Time

Measure time spent creating factory objects:

EVENT_PROF='factory.create' bundle exec rspec

Slow factories often indicate overly complex object graphs or unnecessary associations.

Factory Usage Analysis

Identify all factory usages across the test suite:

FPROF=1 bundle exec rspec

This shows which factories are used most frequently and how much time each consumes.

Factory Flamegraph

Generate a flamegraph visualization of factory usage:

FPROF=flamegraph bundle exec rspec

Flamegraphs help visualize nested factory calls and identify deep object creation chains.

Suboptimal Factory Usage

Check for factories that create more data than necessary:

FDOC=1 bundle exec rspec

This identifies tests that may be using create when build or build_stubbed would suffice.

Factory Default Candidates

Find opportunities to use factory defaults for frequently created objects:

FACTORY_DEFAULT_PROF=1 bundle exec rspec --tag slow:factory

Factory defaults can significantly speed up tests by reusing common objects instead of recreating them.

Setup Time Analysis

Measure time spent in spec setup (before hooks, let blocks, etc.):

RD_PROF=1 bundle exec rspec

High setup time may indicate:

  • Expensive before(:each) hooks that could be before(:all)
  • Complex let definitions that are always evaluated
  • Unnecessary data setup for simple tests

Interpreting Results

When analyzing test performance:

  1. Focus on outliers - Look for specs that take significantly longer than average
  2. Check factory chains - Deep factory associations multiply creation time
  3. Review database activity - Excessive queries often indicate missing let_it_be or factory defaults
  4. Consider test isolation - Some slowness is acceptable for thorough integration tests

References

Clone this wiki locally