pytest: escreva menos, teste mais

46
pytest

Upload: erick-wilder

Post on 18-Aug-2015

155 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Pytest: escreva menos, teste mais

pytest

Page 2: Pytest: escreva menos, teste mais

por quê?

Page 3: Pytest: escreva menos, teste mais

~100K L

Page 4: Pytest: escreva menos, teste mais

menos verboso

Page 5: Pytest: escreva menos, teste mais

import unittest

class SomethingTestCase(unittest.TestCase): def test_something(self): self.assertNotEqual('foobar'.upper(), 'FOOBAR')

if __name__ == '__main__': unittest.main()

Page 6: Pytest: escreva menos, teste mais

import unittest

class SomethingTestCase(unittest.TestCase): def test_something(self): self.assertNotEqual('foobar'.upper(), 'FOOBAR')

if __name__ == '__main__': unittest.main()

Page 7: Pytest: escreva menos, teste mais

import unittest

class SomethingTestCase(unittest.TestCase): def test_something(self): assert 'foobar'.upper() != 'FOOBAR'

if __name__ == '__main__': unittest.main()

Page 8: Pytest: escreva menos, teste mais

import unittest

class SomethingTestCase(unittest.TestCase): def test_something(self): assert 'foobar'.upper() != 'FOOBAR'

if __name__ == '__main__': unittest.main()

Page 9: Pytest: escreva menos, teste mais

import unittest

class SomethingTestCase(unittest.TestCase): def test_something(self): assert 'foobar'.upper() != 'FOOBAR'

Page 10: Pytest: escreva menos, teste mais

import unittest

class SomethingTestCase(unittest.TestCase): def test_something(self): assert 'foobar'.upper() != 'FOOBAR'

Page 11: Pytest: escreva menos, teste mais

import unittest

def test_something(): assert 'foobar'.upper() != 'FOOBAR'

Page 12: Pytest: escreva menos, teste mais

def test_something(): assert 'foobar'.upper() != 'FOOBAR'

Page 13: Pytest: escreva menos, teste mais

import unittest

class SomethingTestCase(unittest.TestCase): def test_something(self): self.assertNotEqual('foobar'.upper(), 'FOOBAR')

if __name__ == '__main__': unittest.main()

Page 14: Pytest: escreva menos, teste mais

linhas: 6 > 2

Page 15: Pytest: escreva menos, teste mais

18K L

Page 16: Pytest: escreva menos, teste mais

relatórios melhores

Page 17: Pytest: escreva menos, teste mais

self.assertEqual('foobar', 'foobaz')

Page 18: Pytest: escreva menos, teste mais

FAIL: test_something (__main__.SomethingTestCase)------------------------------------------------------------------Traceback (most recent call last): File "test_something_verbose.py", line 9, in test_something self.assertEqual('foobar', 'foobaz')AssertionError: 'foobar' != 'foobaz'

------------------------------------------------------------------Ran 1 test in 0.000s

FAILED (failures=1)

Page 19: Pytest: escreva menos, teste mais

assert 'foobar' == 'foobaz'

Page 20: Pytest: escreva menos, teste mais

def test_something():> assert 'foobar' == 'foobaz'E assert 'foobar' == 'foobaz'E - foobarE ? ^E + foobazE ? ^

Page 21: Pytest: escreva menos, teste mais

self.assertEqual({'a': 0, 'b': 1, 'c': 0}, {'a': 0, 'b': 2, 'd': 0})

Page 22: Pytest: escreva menos, teste mais

==================================================================FAIL: test_something (__main__.SomeThingTestCase)------------------------------------------------------------------Traceback (most recent call last): File "test_dict_verbose.py", line 9, in test_something {'a': 0, 'b': 2, 'd': 0})AssertionError: {'c': 2, 'a': 0, 'b': 1} != {'d': 0, 'a': 0, 'b': 2}- {'a': 0, 'b': 1, 'c': 2}? ^ ^ ^

+ {'a': 0, 'b': 2, 'd': 0}? ^ ^ ^

------------------------------------------------------------------Ran 1 test in 0.001s

FAILED (failures=1)

Page 23: Pytest: escreva menos, teste mais

assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0})

Page 24: Pytest: escreva menos, teste mais

def test_something():> assert {'a': 0, 'b': 1, 'c': 2} == {'a': 0, 'b': 2, 'd': 2}E assert {'a': 0, 'b': 1, 'c': 2} == {'a': 0, 'b': 2, 'd': 2}E Omitting 1 identical items, use -v to showE Differing items:E {'b': 1} != {'b': 2}E Left contains more items:E {'c': 2}E Right contains more items:E {'d': 2}E Use -v to get the full diff

Page 25: Pytest: escreva menos, teste mais

fixtures

Page 26: Pytest: escreva menos, teste mais

def test_address(): ...

Page 27: Pytest: escreva menos, teste mais

def test_address(address): assert address.country == 'Brazil'

Page 28: Pytest: escreva menos, teste mais

import pytest

@pytest.fixturedef address(): return Address('Av. Paulista', 'São Paulo', 'SP', 'Brazil')

def test_address(address): assert address.country == 'Brazil'

Page 29: Pytest: escreva menos, teste mais

import pytest

@pytest.fixturedef address(): return Address('Av. Paulista', 'São Paulo', 'SP', 'Brazil')

@pytest.fixturedef place(address): return Place(name='MASP', address=address)

def test_place_street(place): assert place.address.street == 'Av. Paulista'

Page 30: Pytest: escreva menos, teste mais

conftest.py

Page 31: Pytest: escreva menos, teste mais

def test_place_street(place): assert place.address.street == 'Av. Paulista'

Page 32: Pytest: escreva menos, teste mais

@pytest.fixturedef country(): return ‘Brazil’

@pytest.fixturedef address(country): return Address('Av. Paulista', 'São Paulo', 'SP', country)

Page 33: Pytest: escreva menos, teste mais

import pytest

@pytest.fixture(params=['Brazil', 'Germany'])def country(request): return request.param

def test_something(address): assert address.country != 'United States'

Page 34: Pytest: escreva menos, teste mais

fixture scope

Page 35: Pytest: escreva menos, teste mais

import pytest

@pytest.fixture(scope='session')def session_app(): from example.app import create_app return create_app()

@pytest.yield_fixturedef app(session_app): ctx = session_app.test_request_context() ctx.push() yield session_app ctx.pop()

Page 36: Pytest: escreva menos, teste mais

from flask import url_for

def test_requires_app_context(app):

url_for('.home') == '/'

def test_config(app): # Same app used

assert 'SQLALCHEMY_DATABASE_URI' in app.config

Page 37: Pytest: escreva menos, teste mais

assertion helpers

Page 38: Pytest: escreva menos, teste mais

def test_config(app):

assert 'SQLALCHEMY_DATABASE_URI' in app.config

Page 39: Pytest: escreva menos, teste mais

app = <Flask 'example.app'>

def test_config(app):

> assert 'SQLALCHEMY_DATABASE_URI' in app.config

E assert 'SQLALCHEMY_DATABASE_URI' in <Config {'JSON_SORT_KEYS': True, 'SERVER_NAME': None, 'TESTING': False, 'PRESERVE_CONTEXT_ON_EXCEPTION': None, 'DEBUG'...ple.app', 'SESSION_COOKIE_SECURE': False, 'USE_X_SENDFILE': False, 'PREFERRED_URL_SCHEME': 'http', 'SECRET_KEY': None}>

E + where <Config {'JSON_SORT_KEYS': True, 'SERVER_NAME': None, 'TESTING': False, 'PRESERVE_CONTEXT_ON_EXCEPTION': None, 'DEBUG'...ple.app', 'SESSION_COOKIE_SECURE': False, 'USE_X_SENDFILE': False, 'PREFERRED_URL_SCHEME': 'http', 'SECRET_KEY': None}> = <Flask 'example.app'>.config

Page 40: Pytest: escreva menos, teste mais

import pytest

def assert_has_config(app, config_key):

__tracebackhide__ = True

if config_key not in app.config:

found_keys = '\n'.join('- {}'.format(key) for key in app.config.keys())

pytest.fail('"{}" config key not found. Found this instead:\n'

'{}'.format(config_key, found_keys))

Page 41: Pytest: escreva menos, teste mais

app = <Flask 'example.app'>

def test_config_with_helper(app):

> assert_has_config(app, 'SQLALCHEMY_DATABASE_URI')

E Failed: "SQLALCHEMY_DATABASE_URI" config key not found. Found this instead:

E - TESTING

E - JSON_AS_ASCII

E - SESSION_COOKIE_HTTPONLY

E - PRESERVE_CONTEXT_ON_EXCEPTION

E - SESSION_COOKIE_NAME

E - APPLICATION_ROOT

...

E - SESSION_COOKIE_PATH

E - DEBUG

Page 42: Pytest: escreva menos, teste mais

ad populum

Page 43: Pytest: escreva menos, teste mais

plugins

Page 44: Pytest: escreva menos, teste mais

pytest-xdistpytest-cachepytest-django

Page 45: Pytest: escreva menos, teste mais

Resumo:- menos verboso- relatórios melhores- fixtures- assertion helpers- comunidade

Page 46: Pytest: escreva menos, teste mais

twitter.com/erickwilder

github.com/erickwilder

thanks!github.com/erickwilder/pytest-talk

Agradecimento especial a Cesar Canassa por me ensinar tanto sobre py.test