Domanda

I am using behave for acceptance testing of my Django app. Is there any possibility to generate the python code needed for the actual tests? What I have so far is the skeleton as follows:

@given('I am logged in')
def step_impl(context):
    assert False

@given('I am on the contact list')
def step_impl(context):
    assert False

...
È stato utile?

Soluzione

There are examples given on the Django Testing Example page, which will give you something like this as a starting point:

from behave import given, when, then

@given('a user')
def step_impl(context):
    from django.contrib.auth.models import User
    u = User(username='foo', email='foo@example.com')
    u.set_password('bar')

@when('I log in')
def step_impl(context):
    br = context.browser
    br.open(context.browser_url('/account/login/'))
    br.select_form(nr=0)
    br.form['username'] = 'foo'
    br.form['password'] = 'bar'
    br.submit()

@then('I see my account summary')
def step_impl(context):
    br = context.browser
    response = br.response()
    assert response.code == 200
    assert br.geturl().endswith('/account/')

@then('I see a warm and welcoming message')
def step_impl(context):
    # Remember, context.parse_soup() parses the current response in
    # the mechanize browser.
    soup = context.parse_soup()
    msg = str(soup.findAll('h2', attrs={'class': 'welcome'})[0])
    assert "Welcome, foo!" in msg

Can Behave automatically generate the code needed for testing your app? No, not without writing your own logic to do so.

The first step in your testing of the Django app should be to start with writing Feature Files. Once your feature files are complete, you may run behave -d which will give you the stubs/skeleton code for copying into your step files. At this point, you can write scripts to interact with your Django app.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top