1. Lettuce example using scenarios
outline:
El siguiente ejemplo tiene tres archivos, un feature y dos py. En el siguiente ejemplo
atacaremos la api del tiempo y comprobaremos que ciertos valores sean correctos. Para ello
utilizaremos lettuce con python. Además utilizaremos scenarios Outline.
Weather2.feature:
Feature: Check if the weather service works properly
In order to check the weather service
As beginner
I'll get some values and check if they are ok and if the temperature given is correct
Scenario Outline: Check if a city and and country given are correct
Given I access the url with http://api.openweathermap.org/data/2.5/weather
And the city is <city> and the country <country>
When I ask for the city and country name
Then I check if the city and country are correct
And I check if the status code is 200
Examples:
| city | country |
| London | GB |
| Madrid | ES |
| Barcelona | ES |
Scenario Outline: Check if a latitude and longitude given are correct
Given I access the url with http://api.openweathermap.org/data/2.5/weather
And the latitude is <latitude> and the longitude <longitude>
When I ask for the latitude and longitude
Then I check if the latitude and longitude are correct
And I check if the status code is 200
Examples:
| latitude | longitude |
| 51.51 | -0.13 |
| 40.42 | 3.7 |
| 10.13 | -64.7 |
Scenario Outline: Check if the temperature is correct given a city and a country
Given I access the url with http://api.openweathermap.org/data/2.5/weather
And the city is <city> and the country <country>
When I ask for the city and country name
Then I get the temperature look by city and country
Examples:
2. | city | country |
| London | GB |
| Madrid | ES |
| Barcelona | ES |
Scenario Outline: Check if the temperature is correct given the latitude and longitude
Given I access the url with http://api.openweathermap.org/data/2.5/weather
And the latitude is <latitude> and the longitude <longitude>
When I ask for weather information by latitude and longitude
Then I get the temperature look by latitude and longitude
Examples:
| latitude | longitude |
| 51.51 | -0.13 |
| 40.42 | 3.7 |
| 10.13 | -64.7 |
Weather2.py:
__author__ = 'kwiznia'
import requests
from lettuce import world
import json
class weather2():
"""
def getStatus_code(self, response):
world.response = response
world.response = requests.get(response)
return world.response.status_code
"""
def getInformation(self, response):
world.response = requests.get(response)
return world.response.json()
weather2steps.py:
__author__ = 'kwiznia'
from lettuce import *
from weather2 import *
from nose.tools import *
weather2 = weather2()
@step("I access the url with (.*)")
def access_url(self, expectedUrl):
world.expectedUrl = expectedUrl
assert_regexp_matches(world.expectedUrl, '^http'), "The URL is empty"
3. @step('the city is ([^"]+) and the country ([^"]+)')
def city_and_country(self, expectedCity, expectedCountry):
world.expectedCity = expectedCity
world.expectedCountry = expectedCountry
@step("I ask for the city and country name")
def ask_for_city_and_country(self):
world.url = world.expectedUrl + "?q=" + world.expectedCity + "," + world.expectedCountry
world.page_info = weather2.getInformation(world.url)
@step("I check if the city and country are correct")
def check_city_and_country_are_correct(self):
sys = world.page_info['sys']
assert sys != ""
assert world.expectedCity in world.page_info['name']
assert world.expectedCountry in sys['country']
@step("I check if the status code is 200")
def check_status_code(self):
assert_equals(world.response.status_code, 200), "page not found"
@step('the latitude is ([^"]+) and the longitude ([^"]+)')
def check_lat_and_long(self, expectedLatitude, expectedLongitude):
world.expectedLatitude = expectedLatitude
world.expectedLongitude = expectedLongitude
@step("I ask for the latitude and longitude")
def ask_for_lat_and_long(self):
world.url = world.expectedUrl + "?lat=" + world.expectedLatitude + "&lon=" +
world.expectedLongitude
world.page_info = weather2.getInformation(world.url)
@step("I check if the latitude and longitude are correct")
def check_lat_and_long_are_correct(self):
latlon = world.page_info['coord']
assert latlon['lon'] != ""
assert latlon['lat'] != ""
assert_equals(float(world.expectedLatitude), latlon['lat'])
assert_equals(float(world.expectedLongitude), latlon['lon'])
@step("I get the temperature look by city and country")
def get_temperature_look_by_city(self):
temperature = world.page_info['main']
4. temperatuce_farenhait = temperature['temp']
assert temperatuce_farenhait != ""
@step("I ask for weather information by latitude and longitude")
def ask_for_weather_information_by_latlong(self):
world.url = world.expectedUrl + "?lat=" + world.expectedLatitude + "&lon=" +
world.expectedLongitude
world.page_info_by_latlon = weather2.getInformation(world.url)
@step("I get the temperature look by latitude and longitude")
def get_temperature_by_latlong(self):
temperature = world.page_info_by_latlon['main']
temperatuce_farenhait = temperature['temp']
assert temperatuce_farenhait != ""