Coder Social home page Coder Social logo

contest's Introduction

Contest

Contexts for Test::Unit.

Description

Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.

Usage

Declare your tests as you would in RSpec or Shoulda:

require 'contest'

class SomeTest < Test::Unit::TestCase
  setup do
    @value = 1
  end

  teardown do
    @value = nil
  end

  test "sample test" do
    assert_equal 1, @value
  end

  context "a context" do
    setup do
      @value += 1
    end

    test "more tests" do
      assert_equal 2, @value
    end

    context "a nested context" do
      setup do
        @value += 1
      end

      test "yet more tests" do
        assert_equal 3, @value
      end
    end
  end
end

For your convenience, context is aliased as describe and test is aliased as should, so this is valid:

class SomeTest < Test::Unit::TestCase
  setup do
    @value = 1
  end

  describe "something" do
    setup do
      @value += 1
    end

    should "equal 2" do
      assert_equal 2, @value
    end
  end
end

You can run it normally, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:

$ testrb my_test.rb -n test_yet_more_tests

Or with a regular expression:

$ testrb my_test.rb -n /yet_more_tests/

Installation

$ sudo gem install contest

If you want to use it with Rails, add this to config/environment.rb:

config.gem "contest"

Then you can vendor the gem:

rake gems:install
rake gems:unpack

License

Copyright (c) 2009 Damian Janowski and Michel Martens for Citrusbyte

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About Citrusbyte

Citrusbyte

Contest is lovingly maintained and funded by Citrusbyte. At Citrusbyte, we specialize in solving difficult computer science problems for startups and the enterprise.

At Citrusbyte we believe in and support open source software.

Citrusbyte and the Citrusbyte logo are trademarks or registered trademarks of Citrusbyte, LLC.

contest's People

Contributors

cb-frontend avatar djanowski avatar soveran avatar sunaku avatar theshortcut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

contest's Issues

"@controller is nil" error in ActionController::TestCase

When using context() inside an ActionController::TestCase, I get the following error:

RuntimeError: @controller is nil: make sure you set it in your test's setup method.

Here is an example of what my test file looks like:

class ExamplesControllerTest < ActionController::TestCase
  context "index action" do
    should "return a list of examples" do
      get :index, :format => :json
      assert_response :success
    end
  end
end

The error is happening because context() creates a new Test::Unit::TestCase derived class whose name does not contain "ExamplesController", and so Rails becomes confused when it tries to automatically set @controller inside of the context() body.

Thanks for your consideration.

prepend context() title to should() title for focused testing

I think the title passed to context() should be prepended to all child should() titles because it allows for focused testing (passing --name argument to Test::Unit). This would allow you to pass the title of a context() to --name to run all should()s inside that context(). Currently, passing the title of a context() to --name does nothing.

Thanks for your consideration.

Tests run more than once if should blocks follow context blocks

If there are should blocks following context blocks at the same level of nesting, the tests defined by the should blocks are executed more than once. The following example llustrates this:

require 'contest'

class FooTest < Test::Unit::TestCase
  context "a" do
    context "c" do
      should "d" do
        puts "B"
      end

      should "e" do
        puts "C"
      end
    end
    should "b" do
      puts "A"
    end
  end
end

If this code is run, "A" will be printed twice.

Bump the version and gem

The context multiple argument/non-string fix does not appear to be in version 0.1.2 of the gem. Version tags would be nice too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.