This article will do practically the same as the previous (broken link) article, but a file will be created that defines a class that will run the unit tests.
The unit testing part is based on a previous post called Unit tests with unittest in Python (broken link).
The previous articles about docker are:
-
Running microservices with docker using docker-compose (broken link)
-
Starting Django with docker using docker-compose with postgresql as a microservice. (broken link)
-
Creating a Continuous Integration and Deployment environment with Docker for node.js.
-
Using Jupyter Notebook from a Docker container. (broken link)
-
Running a doctest test with a Docker container (broken link).
Now you will have the following files in the pruebas-doctest directory:
pruebas-doctest
├── Dockerfile
├── raizcuadrada.py
├── raizcuadrada_test.py
└── raizcuadrada.txt
The Dockerfile, raizcuadrada.py and raizcuadrada.txt are the same as in the previous article.
The raizcuadrada_test.py file has the following:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Se importa el módulo unittest y math
import unittest
#Se importa la funcion Raiz del modulo raizcuadrada
from raizcuadrada import Raiz
class RaizTest(unittest.TestCase):
def test_Raiz(self):
"""Test para la raiz de nueve que devuelve 3 que debe pasar."""
self.assertEqual(3, Raiz(9))
def test_zero(self):
"""Test para la raiz de 0 que devuelve 0, que debe pasar."""
self.assertEqual(0, Raiz(0))
def test_negative(self):
"""Test para la raiz de un número negativo, que debe fallar."""
# Este debe devolver un ValueError, pero se espera un IndexError.
self.assertRaises(IndexError, Raiz(-10))
if __name__ == '__main__':
#Se ejecuta la prueba unitaria
unittest.main()
Now rebuild the Docker image:
docker build -t pruebas-doctest .
Sending build context to Docker daemon 7.68 kB
Step 1 : FROM python:3.4
---> c40d327867e9
Step 2 : MAINTAINER Ernesto Crespo
---> Using cache
---> 3b1aced33b5e
Step 3 : WORKDIR /app
---> Using cache
---> 7dd09842cf61
Step 4 : COPY . /app
---> 842cd3bd051f
Removing intermediate container 140eaaf7935f
Successfully built 842cd3bd051f
And run the test:
docker run pruebas-doctest python raizcuadrada_test.py
.E.
======================================================================
ERROR: test_negative (__main__.RaizTest)
Test para la raiz de un número negativo, que debe fallar.
----------------------------------------------------------------------
Traceback (most recent call last):
File "raizcuadrada_test.py", line 25, in test_negative
self.assertRaises(IndexError, Raiz(-10))
File "/app/raizcuadrada.py", line 22, in Raiz
raise ValueError("a debe ser >= 0")
ValueError: a debe ser >= 0
----------------------------------------------------------------------
Ran 3 tests in 0.002s
FAILED (errors=1)
This first test was set up to fail. Now the negative test method is commented out, the image is rebuilt and it is run again:
docker build -t pruebas-doctest .
Sending build context to Docker daemon 7.68 kB
Step 1 : FROM python:3.4
---> c40d327867e9
Step 2 : MAINTAINER Ernesto Crespo
---> Using cache
---> 3b1aced33b5e
Step 3 : WORKDIR /app
---> Using cache
---> 7dd09842cf61
Step 4 : COPY . /app
---> 609702415974
Removing intermediate container e2174beb4f7c
Successfully built 609702415974
docker run pruebas-doctest python raizcuadrada_test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
2 tests were run and they passed.
This Docker image can now be reused on several computers, keeping the same development environment.
Make a donation! If you liked the article you can make a donation with Bitcoin (BTC) using the digital wallet of your choice at the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV
Or by scanning the QR code from your wallet:
