Multi-step decorator for Lettuce

Cucumber seriously has me spoiled. Lettuce is great, but it has a lot of catching up to do. I’m glad to see such active progress on it.

One quick tip I wanted to give out was one of how to write steps that can be used with minor verbiage differences. For instance:

Scenario: Make a new post
    Given I am logged out
    When I follow "/posts/new"
    Then I should see "New post"
    And I should see "Create new post"

The “Then” and “And” lines use the same step, but with a special regex to make it happen. Essentially what you want to do is to create a capture group so you can group some or’s, but not actually use the capture group. You can achieve this by using “?:”. The lettuce step looks like this:

@step(u'(?:Then|And) I should see "(.*)"')
def i_should_see(step, text):
    world.res.mustcontain(text)

Hope that helps someone out there.

Using Lettuce and WebTest to Test Your WSGI App

Maybe I’m weird, but I don’t use Django. At my office, we’re using a home brew framework. One thing that is missing from our framework is good testing. I came from a Rails background and one thing I missed was Cucumber. Thankfully, someone was nice enough to make a clone in Python. It’s called Lettuce and you can find out more here.

What I noticed about the Lettuce documentation, being as extensive as it is, there really is no info on how to use it with any other web framework than Django. I was a little discouraged at first, especially when I tried to use the Django’s test client standalone. That wasn’t pretty and I thought all was lost. Persistence led me to Ian Bicking’s WebTest. It’s a fairly simple library to do web testing. For instance: go to this URL, click on this button, and I should see “X”.

With Lettuce and WebTest in hand, I knew it would be feasible to use the two together. I just had to figure out how.

Writing the features is easy. The hard part was getting steps.py to use WebTest so I can interact with my WSGI app just like a browser would:

from lettuce import *
from webtest import TestApp

@before.all
def set_browser():
    world.browser = TestApp(myapp)

@step(u'Given I am a visitor')
def given_i_am_a_visitor(step):
    pass

@step(u'When I access the url "(.*)"')
def access_url(step, url):
    world.response = world.browser.get(url)

@step(u'Then I should see "(.*)"')
def i_should_see(step, text):
    text in world.response

It’s simple, really. The only trickery was to use the @before.all decorator to set the world browser as WebTest’s TestApp. From that point on, I can use WebTest like normal.

Hope this helps someone out there!

Tags: testing python
Waiting for the train

Waiting for the train

Looking towards downtown PDX: 

Standing on the Vista bridge
Looking towards downtown PDX:

Standing on the Vista bridge

Waiting outside for Aziz Ansari

Waiting outside for Aziz Ansari

Aziz Ansari

Aziz Ansari

Cocteau Twins - Box Set No. 1 180g 4xLP: 

Head Over Heels
Garlands
Treasure
Head Over Heels + Sunburst and Snowblind
Cocteau Twins - Box Set No. 1 180g 4xLP:

Head Over Heels
Garlands
Treasure
Head Over Heels + Sunburst and Snowblind

Regression Tagging

I know you run your new albums through Picard, but have you done “regression tagging”? I’ve been spending a little time retagging my massive music library in groups of letters. Most of it is already tagged, so Picard picks up it quickly and i notice that 50% have modifications (e.g. updated year, misspellings, etc)

Also, i did the unthinkable: I cleared all album artwork. That’s right, I selected all 45,000 songs and checked the artwork field with nothing in it and clicked save. It took a few hours to remove them all. I think there were some problems because Picard has an option to embed the artwork in tags (?) I don’t like the sound of it and i think iTunes didn’t like it either.

One other good thing about regression tagging is that I was able to find broken/incomplete albums. Not that I am planning on completing them, but it’s definitely an easy way to find that stuff out.

Discogs Sticker Test: 

testing for new discogs stickers page
Discogs Sticker Test:

testing for new discogs stickers page

Stickers

Stickers