Coder Social home page Coder Social logo

context's Introduction

context – The testing library you’ve been looking for

github.com/jeremymcanally/context

DESCRIPTION:

If you’ve ever wanted contexts in your Test::Unit tests, then context is for you. Your tests will be easier to read and write without all the magic and extra code smell!

FEATURES/PROBLEMS:

  • Add contexts to Test::Unit tests

  • Small DSL for specifying tests that are pretty

  • Ability to chain context lifecycle methods (coming soon)

SYNOPSIS:

  • Add contexts using familiar syntax:

    class UserTest < Test::Unit::TestCase
      context "A new User" do
        # Before/after lifecycle blocks
        before do
          @user = User.first
        end
    
        # Specify tests using DSL
        test "should have the right full_name" do
          assert_equal "Dude Man", @user.full_name
        end
    
        test "should be able to set parts of the name" do
          @user.first_name = "Mad"
          @user.last_name = "Max"
          @user.save
          assert_equal "Mad Max", @user.full_name
        end
    
        after do
          @user.first_name = "Dude"
          @user.last_name = "Man"
          @user.save!
        end
      end
    end
    
  • It also has aliases that match other library’s syntaxes (all of which can be mixed and matched):

    class UserTest < Test::Unit::TestCase
      context "A new Account" do
        test "should be new" do
          Account.new.new_record?
        end
      end
    
      # RSpec-esque
      describe "A new User" do
        it "should do things" do
          User.first.do_things!
        end
      end
    
      # Shoulda-esque
      context "Another User" do
        should "do things that are fun" do
          User.first.do_things!(:fun)
        end
      end
    end
    
  • Contexts can also be nested:

    class UserTest < Test::Unit::TestCase
      context "A new User" do
        context "with clown shoes" do
          test "should squeak" do
            assert_true User.find_by_shoes("clown").squeak?
          end
        end
    
        context "without clown shoes" do
          test "should not squeak" do
            assert_false User.find_by_shoes("dressy").squeak?
          end
        end
      end
    end
    
  • You can also share behavior among contexts:

    class UserTest < Test::Unit::TestCase
      shared "shared things" do
        test "things are shared" do
          # test logic here...
        end
      end
    
      context "the first thing" do
        uses "shared things"
    
        test "other things..." do
          # More testing...
        end
      end
    end
    
  • Shared behaviors can also use RSpec syntax

    class UserTest < Test::Unit::TestCase
      share_examples_for "shared things" do
        it "things are shared" do
          # test logic here...
        end
      end
    
      describe "the first thing" do
        it_should_behave_like "shared things"
    
        it "other things..." do
          # More testing...
        end
      end
    end
    

REQUIREMENTS:

  • Test::Unit (you have it; trust me)

INSTALL:

$ gem sources -a http://gems.github.com
$ sudo gem install jeremymcanally-context

LICENSE:

Copyright © 2008 Jeremy McAnally

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.

ACKNOWLEDGEMENTS:

Original implementation by myself, but heavily tweaked and borrowed from Rails Core and Pratik Naik.

context's People

Contributors

francois avatar jamesgolick avatar jm avatar mhennemeyer avatar technoweenie 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  avatar

context's Issues

before(:all) callback doesn't work properly on 1.8.6

When I place a top-level before(:all) callback, the callback gets run for every context instead of only being run once. Even if I wrap this callback + all the test cases in a context, I still get the same behavior. This is happening on a mac with 1.8.6 using the system's test-unit or test-unit 1.2.3.

character restrictions in context and test strings

I tried writing a context like this:

context "get /sources [0 sources]" do
  ...
end

But I got an error:

NameError: wrong constant name TestGetSources[0Sources]9518620

So I had to rewrite using alphanumeric characters:

context "get /sources with 0 sources" do
  ...
end

This is at least worth a quick mention in the rdoc if nothing else.

And a shout out for this great gem!

segfault with context 0.5.5 / ruby 1.8.6

Boom


~p/gm(master)> rake --trace
(in /Users/rick/p/gm)
** Invoke default (first_time)
** Invoke test (first_time)
** Execute test
Loading schema...
Sinatra::Test is deprecated; use Rack::Test instead.
/Users/rick/.rvm/gems/ruby/1.8.6/gems/jeremymcanally-context-0.5.5/lib/context/suite.rb:8: [BUG] Segmentation fault
ruby 1.8.6 (2009-08-04) [i686-darwin10.2.0]

rake aborted!
Command failed with status (): [/Users/rick/.rvm/ruby-1.8.6-p383/bin/ruby ...]
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:995:in `sh'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:1010:in `call'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:1010:in `sh'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:1094:in `sh'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:1029:in `ruby'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:1094:in `ruby'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake/testtask.rb:117:in `define'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:1112:in `verbose'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake/testtask.rb:102:in `define'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/Users/rick/.rvm/ruby-1.8.6-p383/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:607:in `invoke_prerequisites'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:596:in `invoke_with_call_chain'
/Users/rick/.rvm/ruby-1.8.6-p383/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/rick/.rvm/gems/ruby/1.8.6/gems/rake-0.8.7/bin/rake:31
/Users/rick/.rvm/ruby-1.8.6-p383/bin/rake:22:in `load'
/Users/rick/.rvm/ruby-1.8.6-p383/bin/rake:22

Works with 0.5.2...

quirky single test file execution with test-unit 2.0.3

With this latest version of test-unit, context works fine when tests are run with rake test.
However, when executing individual test files with testrb, no output is emitted. Guessing it's an API change on test-unit's part?
Thanks for the awesome gem!

ruby 1.9.1 issues

I get the following when running context with ruby 1.9.1p243:

test: scrape_with_eval determines arguments with literal defaults(Boson::ArgumentInspectorTest::TestScrapeWithEval3387818):
NameError: undefined local variable or method method_name' for #<Boson::ArgumentInspectorTest::TestScrapeWithEval3387818:0x66a61c> /Users/bozo/.rvm/gems/ruby/1.9.1/gems/jeremymcanally-context-0.5.5/lib/context/lifecycle.rb:63:insetup'
/Users/bozo/.rvm/gems/ruby/1.9.1/gems/mocha-0.9.8/lib/mocha/integration/mini_test/version_131_and_above.rb:25:in `run'

It seems method_name has disappeared in ruby 1.9.1. I tried changing method_name to name but then I get constant creation errors such as this:

test: scrape_with_eval determines arguments with literal defaults(Boson::ArgumentInspectorTest::TestScrapeWithEval3387776):
NameError: uninitialized constant Boson::ArgumentInspectorTest::TestScrapeWithEval3387776::MethodInspector
/Users/bozo/code/gems/boson/test/argument_inspector_test.rb:34:in block (2 levels) in <class:ArgumentInspectorTest>' /Users/bozo/.rvm/gems/ruby/1.9.1/gems/jeremymcanally-context-0.5.5/lib/context/lifecycle.rb:80:ininstance_eval'
/Users/bozo/.rvm/gems/ruby/1.9.1/gems/jeremymcanally-context-0.5.5/lib/context/lifecycle.rb:80:in `block in run_each_callbacks'

Any ideas on how to make context 1.9.1 compatible?

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.