domingo, 5 de junio de 2016

Automatizando las pruebas para Pyramid con Test Funcionales

Las pruebas funcionales son otra onda. La cuestión es que se encuentran un poco más arriba de la aplicación, casi que casi es como hacer las pruebas de toda la vida en el navegador,  así que hay poco control de la aplicación en sí, lo que de hecho no es tan malo después de todo, ya que tampoco el usuario final tendrá tanto control de nuestra aplicación.

Para nuestro proyecto, bastará con que crear el fichero ./ambiente/aplicacion/aplicacion/tests/testFuncionales.py con el siguiente contenido.
#coding:utf-8

from unittest import TestCase

class Listado(TestCase):
    def setUp(self):
        from aplicacion import main
        from webtest import TestApp
        
        app = main({})
        self.testapp = TestApp(app)
    
    def test_ficheros_listado(self):
        respuesta = self.testapp.get('/ficheros', status=200, xhr=True)
        self.assertEqual(respuesta.content_type, 'application/json')
        self.assertItemsEqual(respuesta.json_body['respuesta'], ['alortiz', 'kpenate', 'opineda']) 

class Detalle(TestCase):
    def setUp(self):
        from aplicacion import main
        from webtest import TestApp

        app = main({})
        self.testapp = TestApp(app)

    def test_ficheros_detalle(self):
        respuesta = self.testapp.get('/ficheros/' + 'alortiz', status=200, xhr=True)
        self.assertItemsEqual(respuesta.json_body['respuesta']['palabras'], ['ambiente', 'publico'])

    def test_ficheros_detalle_inexistente(self):
        respuesta = self.testapp.get('/ficheros/' + 'fitzcarraldo', status=200, xhr=True)
        self.assertEqual(respuesta.json_body['respuesta']['error'], 'No such file or directory')

class Creacion(TestCase):
    def setUp(self):
        from aplicacion import main
        from webtest import TestApp

        app = main({})
        self.testapp = TestApp(app)

    def tearDown(self):
        respuesta = self.testapp.delete('/ficheros/' + 'fcornejo', status=200)

    def test_ficheros_creacion(self):
        datos = {'usuario': 'fcornejo', 'data': {'nombre': 'Flor', 'apellido':'Cornejo', 'palabras': ['ente', 'obvio']}}
        respuesta = self.testapp.post_json('/ficheros', status=200, params=datos)
        self.assertDictEqual(respuesta.json_body['respuesta'], datos['data'])

class Modificacion(TestCase):
    def setUp(self):
        from aplicacion import main
        from webtest import TestApp

        app = main({})
        self.testapp = TestApp(app)

        datos = {'usuario': 'fcornejo', 'data': {'nombre': 'Flor', 'apellido':'Cornejo', 'palabras': ['ente', 'obvio']}}
        self.testapp.post_json('/ficheros', status=200, params=datos)
    
    def tearDown(self):
        respuesta = self.testapp.delete('/ficheros/' + 'fcornejo', status=200)
 
    def test_ficheros_modificacion(self):
        datos = {'usuario': 'fcornejo', 'data': {'nombre': 'Flor', 'apellido':'Cornejo', 'palabras': ['espejismo', 'olvido']}}
        respuesta = self.testapp.put_json('/ficheros/' + 'fcornejo', status=200, params=datos)
        self.assertDictEqual(respuesta.json_body['respuesta'], datos['data'])
       
class Borrado(TestCase):
    def setUp(self):
        from aplicacion import main
        from webtest import TestApp

        app = main({})
        self.testapp = TestApp(app)
        
        datos = {'usuario': 'fcornejo', 'data': {'nombre': 'Flor', 'apellido':'Cornejo', 'palabras': ['ente', 'obvio']}}
        self.testapp.post_json('/ficheros', status=200, params=datos)

    def test_ficheros_borrado(self):
        respuesta = self.testapp.delete('/ficheros/' + 'fcornejo', status=200)
        self.assertEqual(respuesta.json_body['respuesta'], 'fcornejo')
        
Al correr nosetest con un poco de verbosidad (Un -v más no añade nada importante, y el próximo es casi preocupante), la salida debería ser la siguiente:
$ nosetests -v
test_ficheros_borrado (aplicacion.tests.testFuncionales.Borrado) ... ok
test_ficheros_creacion (aplicacion.tests.testFuncionales.Creacion) ... ok
test_ficheros_detalle (aplicacion.tests.testFuncionales.Detalle) ... ok
test_ficheros_detalle_inexistente (aplicacion.tests.testFuncionales.Detalle) ... ok
test_ficheros_listado (aplicacion.tests.testFuncionales.Listado) ... ok
test_ficheros_modificacion (aplicacion.tests.testFuncionales.Modificacion) ... ok
test_ficheros_borrado (aplicacion.tests.testUnitarios.Borrado) ... ok
test_ficheros_borrado_inexistente (aplicacion.tests.testUnitarios.Borrado) ... ok
test_ficheros_creacion (aplicacion.tests.testUnitarios.Creacion) ... ok
test_ficheros_creacion_json_malformed (aplicacion.tests.testUnitarios.Creacion) ... ok
test_ficheros_detalle (aplicacion.tests.testUnitarios.Detalle) ... ok
test_ficheros_detalle_noexistente (aplicacion.tests.testUnitarios.Detalle) ... ok
test_ficheros_listado (aplicacion.tests.testUnitarios.Listado) ... ok
test_ficheros_modificacion (aplicacion.tests.testUnitarios.Modificacion) ... ok

Name                                  Stmts   Miss  Cover   Missing
-------------------------------------------------------------------
aplicacion.py                            10      0   100%   
aplicacion/fichero_app.py                 0      0   100%   
aplicacion/fichero_app/ficheros.py       46     11    76%   21-22, 36, 52-55, 69-72
aplicacion/tests.py                       0      0   100%   
aplicacion/tests/testFuncionales.py      60      0   100%   
aplicacion/tests/testUnitarios.py        99      0   100%   
aplicacion/views.py                       0      0   100%   
aplicacion/views/actividades.py          35      2    94%   43-44
-------------------------------------------------------------------
TOTAL                                   250     13    95%   
----------------------------------------------------------------------
Ran 14 tests in 1.231s

OK

        

No hay comentarios:

Publicar un comentario

Otros apuntes interesantes

Otros apuntes interesantes