Back to Repositories

Testing Environment Variable Auto-Restoration in dotenv

This test suite validates the environment variable auto-restoration functionality in the dotenv gem. It ensures that environment variables are properly reset between test runs to maintain test isolation and prevent state leakage.

Test Coverage Overview

The test suite focuses on verifying the automatic restoration of environment variables between test executions.

  • Tests environment variable cleanup between consecutive test runs
  • Validates nil state of ENV variables before each test
  • Verifies isolation of test environments

Implementation Analysis

The testing approach utilizes ActiveSupport::TestCase as the base framework with Minitest integration. It implements a sequential testing pattern to verify state restoration between tests.

  • Uses ActiveSupport test helpers for structured test cases
  • Implements before/after test cleanup verification
  • Leverages dotenv/autorestore module functionality

Technical Details

  • Minitest test framework
  • ActiveSupport::TestCase integration
  • dotenv gem configuration
  • Environment variable manipulation
  • Automatic state restoration hooks

Best Practices Demonstrated

The test suite exemplifies strong testing practices by ensuring test isolation and preventing environmental state leakage between tests.

  • Proper test isolation
  • Environmental state cleanup
  • Clear assertion messages
  • Sequential validation approach

bkeepers/dotenv

test/autorestore_test.rb

            
require "active_support" # Rails 6.1 fails if this is not loaded
require "active_support/test_case"
require "minitest/autorun"

require "dotenv"
require "dotenv/autorestore"

class AutorestoreTest < ActiveSupport::TestCase
  test "restores ENV between tests, part 1" do
    assert_nil ENV["DOTENV"], "ENV was not restored between tests"
    ENV["DOTENV"] = "1"
  end

  test "restores ENV between tests, part 2" do
    assert_nil ENV["DOTENV"], "ENV was not restored between tests"
    ENV["DOTENV"] = "2"
  end
end