Python Unit Testing

Sunday morning, I decided to start this blog post.

So I started watching the course and followed each step.

Python Virtual Environments

I’m doing this course in my Ubuntu machine. I started the tutorial in Windows 10 but… everything is more complicated in Windows for some reason.

I failed creatinv the virtual environment with Python 2.7, but I could do it with Python 3.

First, I installed the python-3-venv dependency. Then I created the virtual environment and activated the source. So, now everything I do is only in the venv.

01

To test it, I installed pytest, I ran the command and it was identified. Then, I deactivated the virtual environment by executing deactivate, I ran pytest againg and the command was not found.

02

Setup pytest in PyCharm

I installed PyCharm Community with the command

sudo snap install pycharm-community --classic

 

I followed the wizard:

  1. Created the project pytest_test
  2. Clicked Next

The IDE created a new Virtual Environment and I decided to continue with it. Next, I created the first failing test.

04

Then, I added pytest to the project by going to Run > Edit Configurations. I clicked on Add New Configuration and I chose the pytest option. I changed the name to Unit Tests and picked the pytest_test.py file in the Target field.

Screenshot from 2019-04-07 19-20-35

Next, I installed pytest in Pycharm using the Terminal.

Screenshot from 2019-04-07 19-27-12

Then, I could run the test by clicking the green play button at the right top of the window. As expected, the test failed.

Screenshot from 2019-04-07 19-27-34

I changed the test and now it passes!

Screenshot from 2019-04-07 19-45-34

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s