Is There a Python Equivalent for Rspec to Do Tdd

Is there a python equivalent for RSpec to do TDD?

The closest I have come to doing a brief google search was mamba + expects:

  • mamba: https://github.com/nestorsalceda/mamba
  • expects: https://github.com/jaimegildesagredo/expects

Am I the *only* one who doesn't see the benefit of the test driven development principles?

Writing the test is more like a way to think about the requirements and how to achieve those things. To my point of view, maybe you need to use some TDD frameworks. I think one of the reasons why you feel difficult doing TDD is Python did not have significant TDD frameworks (Although people are saying: Life is short so I use Python). If you use Ruby and Rspec, you will find TDD is not that tough.

By using some frameworks, you can easily design your test logics and for many of the steps you write today, you can simply reuse them in your future test case.

Hope this can help you:

Is there a python equivalent for RSpec to do TDD?

Also, if you wanna explore more into the TDD world, you can try with BDD. I would recommend you to use Python Behave if you are interested in playing with BDD.

http://pythonhosted.org/behave/

Quick example, say you wanna test the requests library

You can write your test cases like

When I try to request "https://stackoverflow.com/"
Then I could get response code is "200"

And in BDD you can define your steps like

@when('I try to request "{my_url}"')
def step_impl(context, my_url):
context.last_result = requests.get(my_url)

@then('I could get response code is "{code}"')
def step_impl(context, code):
context.last_result.status_code.should.be.eq(code)

Once you hit here, you can simply use your steps tomorrow as

When I try to request "fake_url"
Then I could get response code is "404"

When I try to request "url_needs_autho"
Then I could get response code is "401"

You can now test whatever behavior of your library or function. Life is short so I use Python

Are there any good online tutorials to TDD for an experienced programmer who is new to testing?

One suggestion I'd make is to start a coding Dojo group. It helps to start TDD from scratch with a group, with most of recommended best-practices and focus on TDD.

Its basic ideas is to take a simple challenge (like a program that transforms roman algarisms strings into ints), and start to code it, starting from simple inputs, and coding only when there's a test failing. It's not the focus of this to end the problem, but to start making it the right way.

Here's another link about it, from which I retrieved the following part:

  • There is a coding challenge that is announced beforehand.
  • There is a room with one computer attached to video screen.
  • The presenter explains the coding challenge and starts the coding. The presenter may or may not choose to have a co-pilot. If this is a Randori session, a co-pilot is usually assigned so that when the switch occurs, the co-pilot takes over for the coder.
  • One half of the pair is changed every 5 minutes if the session is Randori.
  • The coder should continuously explain what she or he is doing.
  • The coder should stop when someone from the audience falls off the sled (has a question about understanding what the pair is doing) -- and only continue when that someone is back on track again.
  • All coders use TDD (Test-Driven Development).
  • All produced code will be made publicly available using the Eclipse Common Public License.
  • The programming language to be used is announced in advance per session.

Is there an rspec test for exact length of an attribute?

If you're testing a validation on an ActiveRecord model, I recommend trying out shoulda-matchers. It provides a bunch of useful RSpec extensions useful for Rails. You could write a simple one line spec for your zip code attribute:

describe Address do
it { should ensure_length_of(:zip_code).is_equal_to(5).with_message(/invalid/) }
end

RSpec collection without running tests

There seems to be no built-in way to do collection only. Instead, I had to add the following in my spec_helper. Basically, skip the test if you pass in an environment variable CONTEXT=validate into your test run. Not really a great way to do this but it seems RSpec provides no built-in alternative:

  config.around(:each) do |example|
# validate test case metadata. Ensure proper folder structure and team tagging
# No need to run tests during validation, so they are skipped and left in 'pending' state
if CONTEXT == 'validate'
puts '[INFO] Skipping spec run and validating spec'
example.skip
validate_spec(example)
else
example.run
end

Is there a way to use a matcher like 'Verify' in rspec?

There isn't a way to do this with RSpec, out of the box.
Because Rspec is designed to test small, isolated logics.

On failure, Rspec matchers raise Error, So what you can do is to wrap the matchers in a rescue block.
To satisfy your need, you could write a wrapper like this:

def report_last(&block)
begin
yield
rescue Exception => e
puts "Failure: #{e}"
end
end

In your test case:

describe Calculator do
it “should add 2 numbers” do
report_last do
expect(described_class.new(2, 3).sum)to eq(5)
end
end
end

Practicing BDD with python

Ian Bicking recommends using doctest for behavior driven design:

I personally tend to use nose and voidspace mock in a behavior driven design style. Specifically, the spec plugin for nose is excellent for BDD.

Is there a consensus about test frameworks for Ruby 1.9.x?

Is there consensus? I'd say not. There's a strong community voice for using TDD/BDD but even then it's not universal. The framework is probably less important than applying the practice in the first place.

I believe that minitest is the 1.9 replacement for Test::Unit, which only exists as a wrapper around the new library for backward compatibility. I'd guess that taking the minitest-specific route would be more forward-looking.

RSpec is a somewhat different take: less obviously infused by the "traditional" xUnit-influenced frameworks. It's worth spending some time with to see if you prefer it.

There's also Shoulda. although I don't know how tested it is on 1.9: it's more of a wrapper/extension for Test::Unit, so it'll probably work, though.

Of course the fun doesn't stop there: once you've chosen a test framework you're going to want a mocking engine of some kind. Or a data fixture replacement.

In all cases, I'd say any framework for which you can find evidence of regular updates, an open, populated community and an API you don't hate will do the job. It's unlikely that you're going to be shot down later for having made whatever choice you do.

Rspec rails TDD

Lets try this solution manual
add these gems in your GemFile

gem 'capybara'
gem 'rspec-rails'
gem 'wombat'
gem 'capybara-webkit'
gem 'selenium-webdriver', '2.35.0'

and after that run bundle and to create test envoirment do something like that in your database.yml

defaults: &defaults
adapter: mysql2
host: localhost
username: root
password: password

test:
<<: *defaults
database: test_db

and then run

 rails generate rspec:install 

this will generate these files.

 .rspec
spec/spec_helper.rb
spec/rails_helper.rb

and to run the test cases use this command.

 bundle exec rspec

include these in rspec_helper

 require 'capybara'
require 'capybara/dsl'
require 'debugger'

and these in rails_helper

   config.use_transactional_fixtures = true
config.include Capybara::DSL

then

   RAILS_ENV=test rake db:create, db:migrate

or
RAILS_ENV=test rake db:schema:load

Hopefully this solved your problem.



Related Topics



Leave a reply



Submit