Class Test::Unit::TestCase
In: lib/test/unit/testcase.rb
Parent: Object

Ties everything together. If you subclass and add your own test methods, it takes care of making them into tests and wrapping those tests into a suite. It also does the nitty-gritty of actually running an individual test and collecting its results into a Test::Unit::TestResult object.

You can run two hooks before/after a TestCase run.

Example:

  class TestMyClass < Test::Unit::TestCase
    class << self
      def startup
        ...
      end

      def shutdown
        ...
      end
    end

    def setup
      ...
    end

    def teardown
      ...
    end

    def test_my_method1
      ...
    end

    def test_my_method2
      ...
    end
  end

Here is a call order:

Methods

==   default_test   description   description   interrupted?   name   new   run   setup   shutdown   size   startup   suite   teardown   test   test_order   test_order=   to_s  

Included Modules

Attribute Fixture ExceptionHandler ErrorHandler FailureHandler TestCasePendingSupport TestCaseOmissionSupport TestCaseNotificationSupport Priority Assertions Util::BacktraceFilter Util::Output

Attributes

method_name  [R] 

Public Class methods

Describes a test.

The following example associates "register a normal user" description with "test_register" test.

  description "register a normal user"
  def test_register
    ...
  end

Creates a new instance of the fixture for running the test represented by test_method_name.

Called after every test case runs. Can be used to tear down fixture information used in test case scope.

Here is an example test case:

  class TestMyClass < Test::Unit::TestCase
    class << self
      def shutdown
        ...
      end
    end

    def teardown
      ...
    end

    def test_my_class1
      ...
    end

    def test_my_class2
      ...
    end
  end

Here is a call order:

Note that you should not assume test order. Tests should be worked in any order.

Called before every test case runs. Can be used to set up fixture information used in test case scope.

Here is an example test case:

  class TestMyClass < Test::Unit::TestCase
    class << self
      def startup
        ...
      end
    end

    def setup
      ...
    end

    def test_my_class1
      ...
    end

    def test_my_class2
      ...
    end
  end

Here is a call order:

  • startup
  • setup
  • test_my_class1 (or test_my_class2)
  • setup
  • test_my_class2 (or test_my_class1)

Note that you should not assume test order. Tests should be worked in any order.

Rolls up all of the test* methods in the fixture into one suite, creating a new instance of the fixture for each method.

Defines a test in declarative syntax.

The following two test definitions are the same:

  description "register user"
  def test_register_user
    ...
  end

  test "register user" do
    ...
  end

Returns the current test order. This returns +:alphabetic+ by default.

Sets the current test order.

Here are the available order:

:alphabetic
Default. Tests are sorted in alphabetic order.
:random
Tests are sorted in random order.
:defined
Tests are sorted in defined order.

Public Instance methods

It‘s handy to be able to compare TestCase instances.

Returns a description for the test. A description will be associated by Test::Unit::TestCase.test or Test::Unit::TestCase.description.

Returns a name for the test for no description test.

Returns a human-readable name for the specific test that this instance of TestCase represents.

Runs the individual test method represented by this instance of the fixture, collecting statistics, failures and errors in result.

Called before every test method runs. Can be used to set up fixture information.

You can add additional setup tasks by the following code:

  class TestMyClass < Test::Unit::TestCase
    def setup
      ...
    end

    setup
    def my_setup1
      ...
    end

    setup
    def my_setup2
      ...
    end

    def test_my_class
      ...
    end
  end

Here is a call order:

  • setup
  • my_setup1
  • my_setup2
  • test_my_class

Called after every test method runs. Can be used to tear down fixture information.

You can add additional teardown tasks by the following code:

  class TestMyClass < Test::Unit::TestCase
    def teardown
      ...
    end

    teardown
    def my_teardown1
      ...
    end

    teardown
    def my_teardown2
      ...
    end

    def test_my_class
      ...
    end
  end

Here is a call order:

  • test_my_class
  • my_teardown2
  • my_teardown1
  • teardown

Overridden to return name.

[Validate]