SlideShare a Scribd company logo
1 of 61
Download to read offline
Pytest
recommendations and basic
packages for testing in
Python and Django
Andreu Vallbona
PyBCN June 2019
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Who am I
Andreu Vallbona @avallbona
Bachelor degree in computer science
Web developer at APSL, Mallorca, Spain
Mainly developing with Python and Django
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
What we do at APSL
Web development
Systems engineering - devops
Data science
Mobile apps
Consulting and formation
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Advantages
Advantages
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Advantages
● Ensure the quality of the code
● Confidence when changes/refactors are made
● Facilitate Python and / or Django version
upgrades
● Ease in database system changes (e.g.: from
mysql to postgres)
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Why pytest?
Why pytest?
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Why pytest?
● Very little boilerplate code, which makes tests
easy to write and understand
● Can easily be extended with plugins
● Parametrize any test and cover all uses of a
unit without code duplication
● Uses fixtures as a method to recreate previous
scenario
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Why pytest?
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Recommendations
Recommendations
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Recommendations
● Test database in local and / or memory
● Be careful with the signals of Django
● Mock external services
● Concurrent execution
● Review PEP8
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Recommendations
● Do not overly sophisticate the tests
● Self-contained and independent tests
● Use parametrize
● Use fixtures
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Fixtures are objects/dependencies that we predefine and can then be used
by our test functions. You can define different scopes:
● function level: runs once per test
● class level: runs once per test class
● module level: runs once per per module
● session level: runs once per session
fixtures
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Fixtures can be parametrized
fixtures
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Fixtures can be autoinjected to test functions
fixtures
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Plugins
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest plugin that gives us a whole series of helpers and fixtures very useful
to test projects implemented in Django. Among others we can highlight:
● django_db - gives access to the database
● rf - request factory
● client - test client
● admin_client - authenticated test client
● admin_user - authenticated superuser
● settings - access to the django settings
● mailoutbox - mailbox where to test the sending of emails
pytest-django
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Some key features of this plugin are:
● allow us to set a settings for the test scenario
● allow us to reuse the test database between test sessions
● allow us not to execute the migrations when creates the test database, it
create the test database directly from the models
pytest-django
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
●
pytest-django
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-django
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Allow us to populate easily the database with initial test data
pytest-django
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Package that helps us to easily create fixtures based on django models very
easily
● field values are generated automatically
● random content of the fields but can be specified individually
● You can create objects:
○ in memory (mommy.prepare) useful for unit tests model methods
○ persistent (mommy.make) useful for integration tests
● you can define relationships between objects (fk, m2m)
● you can define recipes, which are like templates
● sequence fields can be defined
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Giving the following model:
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
We could generate an instance of it with:
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
and then use it in a test:
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
example of sequence fields
that would generate 3 instances of the model Administrator and the value of
the name field would be respectively
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
custom generator for specific fields types
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
model mommy or factoryboy
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
model mommy or factoryboy
model-mommy
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Plugin that helps us to use the fixtures in lazy mode, which allows us, for
example, to use the fixtures as parameters with the parametrize.
pytest-lazy-fixture
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Plugin that helps us to use the fixtures in lazy mode, which allows us, for
example, to use the fixtures as parameters with the parametrize.
pytest-lazy-fixture
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
This plugin installs a mocker fixture which is a thin-wrapper around the
patching API provided by the mock package.
Allows us to patch a certain function or method in order to be able to test
our logic given an specific mocked result.
E.g. Test calls to external services, such as a call to an external API.
pytest-mock
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Given a certain method
pytest-mock
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
And an expected result
pytest-mock
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
We can mock the method as:
pytest-mock
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Plugin that helps us verify that we do not leave any breakpoint inserted in our
code. It analyzes the Abstract Syntax Tree of our code.
pytest-checkipdb
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
And warn us
pytest-checkipdb
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Plugin that helps us detect which part of our code is not yet "covered" by the
tests. Allows us to generate reports to easily see untested parts
pytest-cov
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Small case example without testing
pytest-cov
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Plugin that helps us to ensure that our code follows a style guide, for
example, in the case of python, the PEP8
pytest-flake8
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-sugar
Plugin that helps us to change the look & feel of the pytest output
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
freezegun
Package that helps us to "freeze" time. If we have methods that makes use
of the functions:
datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(),
time.time(), time.localtime(), time.gmtime(), time.strftime()
Will always return the moment (date and / or time) in which they have been
frozen.
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
freezegun
Simple example
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
freezegun
We can generate data
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
freezegun
and then check it
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-eradicate
Plugin that detect if we left commented code. Is able to differentiate
commented code from real comments.
Before executing pytest --eradicate
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-eradicate
Plugin that detect if we left commented code. Is able to differentiate
commented code from real comments.
After executing pytest --eradicate
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-eradicate
Plugin that detect if we left commented code. Is able to differentiate
commented code from real comments.
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-xdist
Plugin that allows us to run tests in parallel, allows us to reduce the total
execution time.
It is mandatory that the tests are completely self-contained for the use of
xdist.
When tests are invoked with xdist, pytest-django will create a separate test
database for each process. Each test database will be given a suffix
(something like “gw0”, “gw1”) to map to a xdist process.
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-xdist
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-xdist
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
Package that helps us generate data random for our specification or data
model.
Helps us to test that our code works for any value within a range and not
only for specific cases.
hypothesis
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
hypothesis
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
hypothesis
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-watch
It helps us to re-launch, automatically, the tests when changes have been
detected in the project files
Allows us to execute commands before and after the execution of the tests
and also commands depending on the success or failure of the tests
We use it in combination with the following plugin, pytest-testmon
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-testmon
Select and re-execute only the tests affected by the latest changes, whether
they are changes in business classes or in the tests themselves
https://www.youtube.com/watch?v=1xahPJ_LNXM&feature=youtu.be
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-testmon
Select and re-execute only the tests affected by the latest changes, whether
they are changes in business classes or in the tests themselves
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-splinter
Plugin that allow us to execute browser session for the execution of
functional tests
It gives us a bunch of fixtures
Allow to capture the screen when an error has ocurred
It allows us to execute the tests on remote servers (e.g.: sauce labs)
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-splinter
Allows the execution of javascript inside the test itself
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-splinter
Test example
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Plugins
pytest-splinter
Test example
Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django
Thanks
That’s all.
Thank you!
Questions?
@avallbona

More Related Content

What's hot

Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytestHector Canto
 
Introducing Playwright's New Test Runner
Introducing Playwright's New Test RunnerIntroducing Playwright's New Test Runner
Introducing Playwright's New Test RunnerApplitools
 
Docker fundamentals
Docker fundamentalsDocker fundamentals
Docker fundamentalsAlper Unal
 
Java Test Automation for REST, Web and Mobile
Java Test Automation for REST, Web and MobileJava Test Automation for REST, Web and Mobile
Java Test Automation for REST, Web and MobileElias Nogueira
 
Hacking Jenkins
Hacking JenkinsHacking Jenkins
Hacking JenkinsMiro Cupak
 
Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...
Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...
Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...Vietnam Open Infrastructure User Group
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkArulalan T
 
API Testing following the Test Pyramid
API Testing following the Test PyramidAPI Testing following the Test Pyramid
API Testing following the Test PyramidElias Nogueira
 
[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者
[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者
[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者Shengyou Fan
 
Git ops & Continuous Infrastructure with terra*
Git ops  & Continuous Infrastructure with terra*Git ops  & Continuous Infrastructure with terra*
Git ops & Continuous Infrastructure with terra*Haggai Philip Zagury
 
TestingAR XVI - Allure Test Reporting Framework
TestingAR XVI - Allure Test Reporting FrameworkTestingAR XVI - Allure Test Reporting Framework
TestingAR XVI - Allure Test Reporting FrameworkTestingAR Meetup
 
初探 Kotlin Multiplatform
初探 Kotlin Multiplatform初探 Kotlin Multiplatform
初探 Kotlin MultiplatformShengyou Fan
 
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台Shengyou Fan
 
Canary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS Summit
Canary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS SummitCanary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS Summit
Canary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS SummitAmazon Web Services
 
Jenkins Online Meetup - Automated SLI based Build Validation with Keptn
Jenkins Online Meetup - Automated SLI based Build Validation with KeptnJenkins Online Meetup - Automated SLI based Build Validation with Keptn
Jenkins Online Meetup - Automated SLI based Build Validation with KeptnAndreas Grabner
 
Visual Studio を使わず .NET する
Visual Studio を使わず .NET するVisual Studio を使わず .NET する
Visual Studio を使わず .NET するm ishizaki
 

What's hot (20)

Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
 
Introducing Playwright's New Test Runner
Introducing Playwright's New Test RunnerIntroducing Playwright's New Test Runner
Introducing Playwright's New Test Runner
 
Docker fundamentals
Docker fundamentalsDocker fundamentals
Docker fundamentals
 
Java Test Automation for REST, Web and Mobile
Java Test Automation for REST, Web and MobileJava Test Automation for REST, Web and Mobile
Java Test Automation for REST, Web and Mobile
 
Hacking Jenkins
Hacking JenkinsHacking Jenkins
Hacking Jenkins
 
Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...
Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...
Room 1 - 5 - Thủy Đặng - Load balancing k8s services on baremetal with Cilium...
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
API Testing following the Test Pyramid
API Testing following the Test PyramidAPI Testing following the Test Pyramid
API Testing following the Test Pyramid
 
[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者
[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者
[PHPConf Taiwan 2015] 跟著 Laravel 5.1 一起成為更好的 PHP 開發者
 
Git ops & Continuous Infrastructure with terra*
Git ops  & Continuous Infrastructure with terra*Git ops  & Continuous Infrastructure with terra*
Git ops & Continuous Infrastructure with terra*
 
TestingAR XVI - Allure Test Reporting Framework
TestingAR XVI - Allure Test Reporting FrameworkTestingAR XVI - Allure Test Reporting Framework
TestingAR XVI - Allure Test Reporting Framework
 
初探 Kotlin Multiplatform
初探 Kotlin Multiplatform初探 Kotlin Multiplatform
初探 Kotlin Multiplatform
 
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
 
Canary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS Summit
Canary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS SummitCanary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS Summit
Canary Deployments on Amazon EKS with Istio - SRV305 - Chicago AWS Summit
 
Jenkins Online Meetup - Automated SLI based Build Validation with Keptn
Jenkins Online Meetup - Automated SLI based Build Validation with KeptnJenkins Online Meetup - Automated SLI based Build Validation with Keptn
Jenkins Online Meetup - Automated SLI based Build Validation with Keptn
 
Visual Studio を使わず .NET する
Visual Studio を使わず .NET するVisual Studio を使わず .NET する
Visual Studio を使わず .NET する
 
Selenium
SeleniumSelenium
Selenium
 
Princípios SOLID
Princípios SOLIDPrincípios SOLID
Princípios SOLID
 
Jenkins
JenkinsJenkins
Jenkins
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 

Similar to PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Django

DIY in 5 Minutes: Testing Django App with Pytest
DIY in 5 Minutes: Testing Django App with Pytest DIY in 5 Minutes: Testing Django App with Pytest
DIY in 5 Minutes: Testing Django App with Pytest Inexture Solutions
 
Py Day Mallorca - Pipenv - Python Dev Workflow for Humans
Py Day Mallorca  - Pipenv - Python Dev Workflow for HumansPy Day Mallorca  - Pipenv - Python Dev Workflow for Humans
Py Day Mallorca - Pipenv - Python Dev Workflow for HumansAndreu Vallbona Plazas
 
Python Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaPython Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaEdureka!
 
PyBCN - pipenv - python dev workflow for humans
PyBCN - pipenv - python dev workflow for humansPyBCN - pipenv - python dev workflow for humans
PyBCN - pipenv - python dev workflow for humansAndreu Vallbona Plazas
 
High scalable applications with Python
High scalable applications with PythonHigh scalable applications with Python
High scalable applications with PythonGiuseppe Broccolo
 
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74Ivy Rueb
 
Intro To JavaScript
Intro To JavaScriptIntro To JavaScript
Intro To JavaScriptIvy Rueb
 
What We Learned Building an R-Python Hybrid Predictive Analytics Pipeline
What We Learned Building an R-Python Hybrid Predictive Analytics PipelineWhat We Learned Building an R-Python Hybrid Predictive Analytics Pipeline
What We Learned Building an R-Python Hybrid Predictive Analytics PipelineWork-Bench
 
Scientist meets web dev: how Python became the language of data
Scientist meets web dev: how Python became the language of dataScientist meets web dev: how Python became the language of data
Scientist meets web dev: how Python became the language of dataGael Varoquaux
 
Master Python.pdf
Master Python.pdfMaster Python.pdf
Master Python.pdfUncodemy
 
Python Certification | Data Science with Python Certification | Python Online...
Python Certification | Data Science with Python Certification | Python Online...Python Certification | Data Science with Python Certification | Python Online...
Python Certification | Data Science with Python Certification | Python Online...Edureka!
 
Complete python toolbox for modern developers
Complete python toolbox for modern developersComplete python toolbox for modern developers
Complete python toolbox for modern developersJan Giacomelli
 
Foshan Rayven lighting Co.,Ltd
Foshan Rayven lighting Co.,LtdFoshan Rayven lighting Co.,Ltd
Foshan Rayven lighting Co.,LtdCaesar Chan
 
Akash rajguru project report sem v
Akash rajguru project report sem vAkash rajguru project report sem v
Akash rajguru project report sem vAkash Rajguru
 
DA 592 - Term Project Report - Berker Kozan Can Koklu
DA 592 - Term Project Report - Berker Kozan Can KokluDA 592 - Term Project Report - Berker Kozan Can Koklu
DA 592 - Term Project Report - Berker Kozan Can KokluCan Köklü
 
Introduction to development with Django web framework
Introduction to development with Django web frameworkIntroduction to development with Django web framework
Introduction to development with Django web frameworkSammy Fung
 
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...Edureka!
 
Django Article V0
Django Article V0Django Article V0
Django Article V0Udi Bauman
 

Similar to PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Django (20)

DIY in 5 Minutes: Testing Django App with Pytest
DIY in 5 Minutes: Testing Django App with Pytest DIY in 5 Minutes: Testing Django App with Pytest
DIY in 5 Minutes: Testing Django App with Pytest
 
Py Day Mallorca - Pipenv - Python Dev Workflow for Humans
Py Day Mallorca  - Pipenv - Python Dev Workflow for HumansPy Day Mallorca  - Pipenv - Python Dev Workflow for Humans
Py Day Mallorca - Pipenv - Python Dev Workflow for Humans
 
Python Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaPython Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | Edureka
 
PyBCN - pipenv - python dev workflow for humans
PyBCN - pipenv - python dev workflow for humansPyBCN - pipenv - python dev workflow for humans
PyBCN - pipenv - python dev workflow for humans
 
High scalable applications with Python
High scalable applications with PythonHigh scalable applications with Python
High scalable applications with Python
 
Django
DjangoDjango
Django
 
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74
 
Intro To JavaScript
Intro To JavaScriptIntro To JavaScript
Intro To JavaScript
 
What We Learned Building an R-Python Hybrid Predictive Analytics Pipeline
What We Learned Building an R-Python Hybrid Predictive Analytics PipelineWhat We Learned Building an R-Python Hybrid Predictive Analytics Pipeline
What We Learned Building an R-Python Hybrid Predictive Analytics Pipeline
 
Scientist meets web dev: how Python became the language of data
Scientist meets web dev: how Python became the language of dataScientist meets web dev: how Python became the language of data
Scientist meets web dev: how Python became the language of data
 
Master Python.pdf
Master Python.pdfMaster Python.pdf
Master Python.pdf
 
Python Certification | Data Science with Python Certification | Python Online...
Python Certification | Data Science with Python Certification | Python Online...Python Certification | Data Science with Python Certification | Python Online...
Python Certification | Data Science with Python Certification | Python Online...
 
Complete python toolbox for modern developers
Complete python toolbox for modern developersComplete python toolbox for modern developers
Complete python toolbox for modern developers
 
Django
Django Django
Django
 
Foshan Rayven lighting Co.,Ltd
Foshan Rayven lighting Co.,LtdFoshan Rayven lighting Co.,Ltd
Foshan Rayven lighting Co.,Ltd
 
Akash rajguru project report sem v
Akash rajguru project report sem vAkash rajguru project report sem v
Akash rajguru project report sem v
 
DA 592 - Term Project Report - Berker Kozan Can Koklu
DA 592 - Term Project Report - Berker Kozan Can KokluDA 592 - Term Project Report - Berker Kozan Can Koklu
DA 592 - Term Project Report - Berker Kozan Can Koklu
 
Introduction to development with Django web framework
Introduction to development with Django web frameworkIntroduction to development with Django web framework
Introduction to development with Django web framework
 
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...
 
Django Article V0
Django Article V0Django Article V0
Django Article V0
 

More from Andreu Vallbona Plazas (7)

Localhost to the internet
Localhost to the internetLocalhost to the internet
Localhost to the internet
 
Pipenv python dev workflow for humans
Pipenv  python dev workflow for humansPipenv  python dev workflow for humans
Pipenv python dev workflow for humans
 
Apsl attrs
Apsl   attrsApsl   attrs
Apsl attrs
 
Apsl pycharm + docker
Apsl   pycharm + dockerApsl   pycharm + docker
Apsl pycharm + docker
 
Apsl testing
Apsl   testingApsl   testing
Apsl testing
 
Apsl translation manager
Apsl   translation managerApsl   translation manager
Apsl translation manager
 
Pytest - testing tips and useful plugins
Pytest - testing tips and useful pluginsPytest - testing tips and useful plugins
Pytest - testing tips and useful plugins
 

Recently uploaded

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 

Recently uploaded (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 

PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Django

  • 1. Pytest recommendations and basic packages for testing in Python and Django Andreu Vallbona PyBCN June 2019
  • 2. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Who am I Andreu Vallbona @avallbona Bachelor degree in computer science Web developer at APSL, Mallorca, Spain Mainly developing with Python and Django
  • 3. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django What we do at APSL Web development Systems engineering - devops Data science Mobile apps Consulting and formation
  • 4. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Advantages Advantages
  • 5. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Advantages ● Ensure the quality of the code ● Confidence when changes/refactors are made ● Facilitate Python and / or Django version upgrades ● Ease in database system changes (e.g.: from mysql to postgres)
  • 6. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Why pytest? Why pytest?
  • 7. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Why pytest? ● Very little boilerplate code, which makes tests easy to write and understand ● Can easily be extended with plugins ● Parametrize any test and cover all uses of a unit without code duplication ● Uses fixtures as a method to recreate previous scenario
  • 8. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Why pytest?
  • 9. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Recommendations Recommendations
  • 10. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Recommendations ● Test database in local and / or memory ● Be careful with the signals of Django ● Mock external services ● Concurrent execution ● Review PEP8
  • 11. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Recommendations ● Do not overly sophisticate the tests ● Self-contained and independent tests ● Use parametrize ● Use fixtures
  • 12. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Fixtures are objects/dependencies that we predefine and can then be used by our test functions. You can define different scopes: ● function level: runs once per test ● class level: runs once per test class ● module level: runs once per per module ● session level: runs once per session fixtures
  • 13. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Fixtures can be parametrized fixtures
  • 14. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Fixtures can be autoinjected to test functions fixtures
  • 15. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Plugins
  • 16. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest plugin that gives us a whole series of helpers and fixtures very useful to test projects implemented in Django. Among others we can highlight: ● django_db - gives access to the database ● rf - request factory ● client - test client ● admin_client - authenticated test client ● admin_user - authenticated superuser ● settings - access to the django settings ● mailoutbox - mailbox where to test the sending of emails pytest-django
  • 17. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Some key features of this plugin are: ● allow us to set a settings for the test scenario ● allow us to reuse the test database between test sessions ● allow us not to execute the migrations when creates the test database, it create the test database directly from the models pytest-django
  • 18. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins ● pytest-django
  • 19. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-django
  • 20. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Allow us to populate easily the database with initial test data pytest-django
  • 21. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Package that helps us to easily create fixtures based on django models very easily ● field values are generated automatically ● random content of the fields but can be specified individually ● You can create objects: ○ in memory (mommy.prepare) useful for unit tests model methods ○ persistent (mommy.make) useful for integration tests ● you can define relationships between objects (fk, m2m) ● you can define recipes, which are like templates ● sequence fields can be defined model-mommy
  • 22. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Giving the following model: model-mommy
  • 23. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins We could generate an instance of it with: model-mommy
  • 24. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins and then use it in a test: model-mommy
  • 25. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins example of sequence fields that would generate 3 instances of the model Administrator and the value of the name field would be respectively model-mommy
  • 26. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins custom generator for specific fields types model-mommy
  • 27. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins model mommy or factoryboy model-mommy
  • 28. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins model mommy or factoryboy model-mommy
  • 29. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Plugin that helps us to use the fixtures in lazy mode, which allows us, for example, to use the fixtures as parameters with the parametrize. pytest-lazy-fixture
  • 30. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Plugin that helps us to use the fixtures in lazy mode, which allows us, for example, to use the fixtures as parameters with the parametrize. pytest-lazy-fixture
  • 31. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins This plugin installs a mocker fixture which is a thin-wrapper around the patching API provided by the mock package. Allows us to patch a certain function or method in order to be able to test our logic given an specific mocked result. E.g. Test calls to external services, such as a call to an external API. pytest-mock
  • 32. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Given a certain method pytest-mock
  • 33. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins And an expected result pytest-mock
  • 34. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins We can mock the method as: pytest-mock
  • 35. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Plugin that helps us verify that we do not leave any breakpoint inserted in our code. It analyzes the Abstract Syntax Tree of our code. pytest-checkipdb
  • 36. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins And warn us pytest-checkipdb
  • 37. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Plugin that helps us detect which part of our code is not yet "covered" by the tests. Allows us to generate reports to easily see untested parts pytest-cov
  • 38. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Small case example without testing pytest-cov
  • 39. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Plugin that helps us to ensure that our code follows a style guide, for example, in the case of python, the PEP8 pytest-flake8
  • 40. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-sugar Plugin that helps us to change the look & feel of the pytest output
  • 41. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins freezegun Package that helps us to "freeze" time. If we have methods that makes use of the functions: datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), time.strftime() Will always return the moment (date and / or time) in which they have been frozen.
  • 42. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins freezegun Simple example
  • 43. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins freezegun We can generate data
  • 44. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins freezegun and then check it
  • 45. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-eradicate Plugin that detect if we left commented code. Is able to differentiate commented code from real comments. Before executing pytest --eradicate
  • 46. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-eradicate Plugin that detect if we left commented code. Is able to differentiate commented code from real comments. After executing pytest --eradicate
  • 47. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-eradicate Plugin that detect if we left commented code. Is able to differentiate commented code from real comments.
  • 48. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-xdist Plugin that allows us to run tests in parallel, allows us to reduce the total execution time. It is mandatory that the tests are completely self-contained for the use of xdist. When tests are invoked with xdist, pytest-django will create a separate test database for each process. Each test database will be given a suffix (something like “gw0”, “gw1”) to map to a xdist process.
  • 49. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-xdist
  • 50. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-xdist
  • 51. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins Package that helps us generate data random for our specification or data model. Helps us to test that our code works for any value within a range and not only for specific cases. hypothesis
  • 52. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins hypothesis
  • 53. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins hypothesis
  • 54. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-watch It helps us to re-launch, automatically, the tests when changes have been detected in the project files Allows us to execute commands before and after the execution of the tests and also commands depending on the success or failure of the tests We use it in combination with the following plugin, pytest-testmon
  • 55. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-testmon Select and re-execute only the tests affected by the latest changes, whether they are changes in business classes or in the tests themselves https://www.youtube.com/watch?v=1xahPJ_LNXM&feature=youtu.be
  • 56. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-testmon Select and re-execute only the tests affected by the latest changes, whether they are changes in business classes or in the tests themselves
  • 57. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-splinter Plugin that allow us to execute browser session for the execution of functional tests It gives us a bunch of fixtures Allow to capture the screen when an error has ocurred It allows us to execute the tests on remote servers (e.g.: sauce labs)
  • 58. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-splinter Allows the execution of javascript inside the test itself
  • 59. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-splinter Test example
  • 60. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Plugins pytest-splinter Test example
  • 61. Andreu Vallbona - PyBCN - June 2019Pytest: recommendations and basic packages for testing in Python and Django Thanks That’s all. Thank you! Questions? @avallbona