Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:06:10+00:00 2026-05-23T21:06:10+00:00

Using the built-in Ruby Minitest framework, is there a way to run some code

  • 0

Using the built-in Ruby Minitest framework, is there a way to run some code once before the entire suite runs, or even once before an entire TestClass runs? I see in the answer to this question that Test::Unit::after_tests can be used to run code after all tests have been run; is there a similar method to run code before all tests have run?

I would like to use this functionality to initialize a test database before the tests run and tear it down after they have all run.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:06:11+00:00Added an answer on May 23, 2026 at 9:06 pm

    This is modified from the MiniTest docs (under Customizable Test Runner Types).

    class Burger
      def initialize
        puts "YOU CREATED A BURGER"
      end
    
      def has_cheese?
        true
      end
    
      def has_pickle?
        false
      end
    end
    
    gem 'minitest'
    
    require 'minitest/unit'
    MiniTest::Unit.autorun
    
    class MyMiniTest
      class Unit < MiniTest::Unit
    
        def before_suites
          # code to run before the first test
          p "Before everything"
        end
    
        def after_suites
          # code to run after the last test
          p "After everything"
        end
    
        def _run_suites(suites, type)
          begin
            before_suites
            super(suites, type)
          ensure
            after_suites
          end
        end
    
        def _run_suite(suite, type)
          begin
            suite.before_suite if suite.respond_to?(:before_suite)
            super(suite, type)
          ensure
            suite.after_suite if suite.respond_to?(:after_suite)
          end
        end
    
      end
    end
    
    MiniTest::Unit.runner = MyMiniTest::Unit.new
    
    class BurgerTest < MiniTest::Unit::TestCase
    
      def self.before_suite
        p "hi"
      end
    
      def self.after_suite
        p "bye"
      end
    
      def setup
        @burger = Burger.new
      end
    
      def test_has_cheese
        assert_equal true, @burger.has_cheese?
      end
    
      def test_has_pickle
        assert_equal false, @burger.has_pickle?
      end
    
    end
    

    Note that you I included gem 'minitest' to use the gem instead of the bundled version which didn’t have the MiniTest::Unit.runner method. Here’s the output.

    Run options: --seed 49053
    
    # Running tests:
    
    "Before everything"
    "hi"
    YOU CREATED A BURGER
    .YOU CREATED A BURGER
    ."bye"
    "After everything"
    
    
    Finished tests in 0.000662s, 3021.1480 tests/s, 3021.1480 assertions/s.
    
    2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
    

    So it calls #setup twice, but .before_suite and .after_suite only once, which is what you are looking for I think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's a smart way of using ActiveSupport or perhaps regular built in Ruby functionality
Is there an in-built function in Ruby to read the whole file without using
I'm generating some CSV output using Ruby's built-in CSV. Everything works fine, but the
I'm testing some javascript libraries I've built by using ruby's harmony . Everything is
I'm using the Ruby SVN bindings built with SWIG. Here's a little tutorial. When
I have built a very simple blog application using Ruby on Rails. New to
Using the built-in Address Book framework for iPhone, how can I save a specific
I'm using webrick (the built-in ruby webserver) to serve .rhtml files (html with ruby
I have built a blog using Ruby on Rails. New to both. I am
I have built a blog application using Ruby on Rails. In the application I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.