by Andrea Stagi
Head of Development @ Lotrèk
https://docs.pytest.org/en/latest/
(Later.......)
from mylib.unicornprinter import uniformat
def test_uniformat():
assert uniformat('Hello everyone!') == '🦄 Hello everyone!'
def test_uniformat_error():
assert uniformat('Wrong!', 'E') == '🚨 Wrong!'
def test_uniformat_warning():
assert uniformat('Pay attention!', 'W') == '⚠️ Pay attention!'
def test_uniformat_nonsense():
assert uniformat('Nonsense type!', 'NNS') == '🦄 Nonsense type!'
test_unicornprinter.py
def test_get_temperature_by_city(mocker):
mocked_resp = {
"main":{
"temp":279.61,
"pressure":1071,
"humidity":100,
"temp_min":272.31,
"temp_max":282.31
},
}
mocked_requests_get = mocker.patch('mylib.weather.requests.get')
mocked_requests_get.return_value.status_code = 200
mocked_requests_get.return_value.json.return_value = mocked_resp
temperature = get_temperature_by_city('Pistoia')
assert temperature['temp'] == 279.61
assert temperature['pressure'] == 1071
assert temperature['humidity'] == 100
assert temperature['temp_min'] == 272.31
assert temperature['temp_max'] == 282.31
def test_get_temperature_by_city_connection_error(mocker):
mocked_requests_get = mocker.patch('mylib.weather.requests.get')
mocked_requests_get.side_effect = requests.exceptions.ConnectionError
with pytest.raises(WeatherConnectionError):
temperature = get_temperature_by_city('Pistoia')
def test_get_temperature_by_city_api_error(mocker):
mocked_requests_get = mocker.patch('mylib.weather.requests.get')
mocked_requests_get.return_value.status_code = 401
with pytest.raises(WeatherAPIError):
temperature = get_temperature_by_city('Pistoia')
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
def weather_index(request):
return render(request, 'weather/index.html')
from django.http import JsonResponse
from django.shortcuts import render
from mylib.weather import get_temperature_by_city
from mylib.weather import WeatherAPIError
def get_temperature_from_city(request):
try:
temperature = get_temperature_by_city(
request.GET.get('city', '')
)
return JsonResponse(temperature)
except WeatherAPIError as ex:
return JsonResponse(
{
'details' : 'Something \'s wrong, please try again!'
},
status=500
)
import pytest
from django.urls import reverse
def test_main_view(client):
url = reverse('weather-index')
response = client.get(url)
assert response.status_code == 200
assert 'Weather Яeport .' in response.content.decode()
def test_page_api_integration(live_server, selenium, mocker):
mocked_get_weather = mocker.patch('api.views.get_temperature_by_city')
mocked_get_weather.return_value = {
'temp' : 100,
'pressure' : 40,
'humidity' : 50,
'temp_min' : 30,
'temp_max' : 40,
}
selenium.get(live_server.url)
selenium.maximize_window()
cityText = selenium.find_element_by_id('inputCity')
cityText.send_keys('Pistoia')
startSearchButton = selenium.find_element_by_id('startSearch')
startSearchButton.click()
time.sleep(5)
tempResultLabel = selenium.find_element_by_id('tempResults')
assert tempResultLabel.text == 'In Pistoia there are 100°F'
time.sleep(5)
A R M A
http://browserify.org/
https://jasmine.github.io/
(Behavior Driven Development)
function helloWorld() {
return 'Hello world';
}
function helloDear(name) {
return 'Hello ' + name;
}
describe('Greetings', function () {
it('says hello to the world', function () {
expect(helloWorld()).toEqual('Hello world');
});
it('says hello to someone', function () {
expect(helloDear('Andrea')).toEqual(
'Hello Andrea'
);
});
});
https://karma-runner.github.io/
Karma
Jasmine
Our test files
Our source files
Run
Using
Import
@astagi
@4stagi
stagi.andrea@gmail.com
📋 slides.com/andreastagi/web-app-tdd
💻 github.com/astagi/testing-web-app-tdd