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

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
1 of 61

Recommended

.net framework by
.net framework.net framework
.net frameworkRam Sagar Mourya
4K views19 slides
Microsoft .NET Framework by
Microsoft .NET FrameworkMicrosoft .NET Framework
Microsoft .NET Frameworkchandrasekhardesireddi
1.3K views60 slides
7 software design patterns you should know in 2022 by
7 software design patterns you should know in 20227 software design patterns you should know in 2022
7 software design patterns you should know in 2022Growth Natives
111 views1 slide
Introduction To Git For Version Control Architecture And Common Commands Comp... by
Introduction To Git For Version Control Architecture And Common Commands Comp...Introduction To Git For Version Control Architecture And Common Commands Comp...
Introduction To Git For Version Control Architecture And Common Commands Comp...SlideTeam
313 views22 slides
Python by
PythonPython
PythonMohammad Junaid Khan
901 views10 slides
Introduction To C# by
Introduction To C#Introduction To C#
Introduction To C#rahulsahay19
2K views17 slides

More Related Content

What's hot

Devops Devops Devops, at Froscon by
Devops Devops Devops, at FrosconDevops Devops Devops, at Froscon
Devops Devops Devops, at FrosconKris Buytaert
8K views49 slides
Fixing Dockerfile Smells: An Empirical Study (ICSME 2022) by
Fixing Dockerfile Smells: An Empirical Study (ICSME 2022)Fixing Dockerfile Smells: An Empirical Study (ICSME 2022)
Fixing Dockerfile Smells: An Empirical Study (ICSME 2022)Giovanni Rosa
87 views32 slides
Case Study: Migration to GitLab (from Bitbucket) at AppsFlyer by
Case Study: Migration to GitLab (from Bitbucket) at AppsFlyerCase Study: Migration to GitLab (from Bitbucket) at AppsFlyer
Case Study: Migration to GitLab (from Bitbucket) at AppsFlyerNoa Harel
724 views39 slides
Applications in Pharo by
Applications in PharoApplications in Pharo
Applications in PharoESUG
124 views39 slides
Python Programming Language | Python Classes | Python Tutorial | Python Train... by
Python Programming Language | Python Classes | Python Tutorial | Python Train...Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...Edureka!
4.9K views48 slides
Dotnet Frameworks Version History by
Dotnet Frameworks Version HistoryDotnet Frameworks Version History
Dotnet Frameworks Version Historyvoltaincx
14.2K views23 slides

What's hot(20)

Devops Devops Devops, at Froscon by Kris Buytaert
Devops Devops Devops, at FrosconDevops Devops Devops, at Froscon
Devops Devops Devops, at Froscon
Kris Buytaert8K views
Fixing Dockerfile Smells: An Empirical Study (ICSME 2022) by Giovanni Rosa
Fixing Dockerfile Smells: An Empirical Study (ICSME 2022)Fixing Dockerfile Smells: An Empirical Study (ICSME 2022)
Fixing Dockerfile Smells: An Empirical Study (ICSME 2022)
Giovanni Rosa87 views
Case Study: Migration to GitLab (from Bitbucket) at AppsFlyer by Noa Harel
Case Study: Migration to GitLab (from Bitbucket) at AppsFlyerCase Study: Migration to GitLab (from Bitbucket) at AppsFlyer
Case Study: Migration to GitLab (from Bitbucket) at AppsFlyer
Noa Harel724 views
Applications in Pharo by ESUG
Applications in PharoApplications in Pharo
Applications in Pharo
ESUG124 views
Python Programming Language | Python Classes | Python Tutorial | Python Train... by Edureka!
Python Programming Language | Python Classes | Python Tutorial | Python Train...Python Programming Language | Python Classes | Python Tutorial | Python Train...
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Edureka!4.9K views
Dotnet Frameworks Version History by voltaincx
Dotnet Frameworks Version HistoryDotnet Frameworks Version History
Dotnet Frameworks Version History
voltaincx14.2K views
Git e Github para Iniciantes by Loiane Groner
Git e Github para IniciantesGit e Github para Iniciantes
Git e Github para Iniciantes
Loiane Groner21.8K views
Software Engineering - Software Models by Reddhi Basu
Software Engineering - Software ModelsSoftware Engineering - Software Models
Software Engineering - Software Models
Reddhi Basu7K views
How To Learn Programming For Beginners | How To Start Coding | Learn Programm... by Simplilearn
How To Learn Programming For Beginners | How To Start Coding | Learn Programm...How To Learn Programming For Beginners | How To Start Coding | Learn Programm...
How To Learn Programming For Beginners | How To Start Coding | Learn Programm...
Simplilearn459 views
Flutter vs React Native | Edureka by Edureka!
Flutter vs React Native | EdurekaFlutter vs React Native | Edureka
Flutter vs React Native | Edureka
Edureka!735 views
OOP Introduction with java programming language by Md.Al-imran Roton
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
Md.Al-imran Roton3.2K views
C# Common Type System & Common Language Specification by Prem Kumar Badri
C# Common Type System & Common Language Specification C# Common Type System & Common Language Specification
C# Common Type System & Common Language Specification
Prem Kumar Badri467 views
Dotnet Basics Presentation by Sudhakar Sharma
Dotnet Basics PresentationDotnet Basics Presentation
Dotnet Basics Presentation
Sudhakar Sharma18.6K views
Introduction to .net framework by Arun Prasad
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
Arun Prasad88.2K views
Spring Boot Interview Questions | Edureka by Edureka!
Spring Boot Interview Questions | EdurekaSpring Boot Interview Questions | Edureka
Spring Boot Interview Questions | Edureka
Edureka!999 views

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

DIY in 5 Minutes: Testing Django App with Pytest by
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
56 views8 slides
Py Day Mallorca - Pipenv - Python Dev Workflow for Humans by
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
239 views74 slides
Python Interview Questions And Answers 2019 | Edureka by
Python Interview Questions And Answers 2019 | EdurekaPython Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaEdureka!
2.1K views42 slides
PyBCN - pipenv - python dev workflow for humans by
PyBCN - pipenv - python dev workflow for humansPyBCN - pipenv - python dev workflow for humans
PyBCN - pipenv - python dev workflow for humansAndreu Vallbona Plazas
233 views70 slides
High scalable applications with Python by
High scalable applications with PythonHigh scalable applications with Python
High scalable applications with PythonGiuseppe Broccolo
325 views33 slides
Django by
DjangoDjango
Djangochaitanayasethi
279 views20 slides

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

DIY in 5 Minutes: Testing Django App with Pytest by Inexture Solutions
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
Python Interview Questions And Answers 2019 | Edureka by Edureka!
Python Interview Questions And Answers 2019 | EdurekaPython Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | Edureka
Edureka!2.1K views
Deck 8983a1d9-68df-4447-8481-3b4fd0de734c-9-52-74 by Ivy Rueb
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
Ivy Rueb106 views
Intro To JavaScript by Ivy Rueb
Intro To JavaScriptIntro To JavaScript
Intro To JavaScript
Ivy Rueb738 views
What We Learned Building an R-Python Hybrid Predictive Analytics Pipeline by Work-Bench
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
Work-Bench11K views
Scientist meets web dev: how Python became the language of data by Gael Varoquaux
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
Gael Varoquaux8.2K views
Python Certification | Data Science with Python Certification | Python Online... by Edureka!
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!498 views
Complete python toolbox for modern developers by Jan Giacomelli
Complete python toolbox for modern developersComplete python toolbox for modern developers
Complete python toolbox for modern developers
Jan Giacomelli125 views
Foshan Rayven lighting Co.,Ltd by Caesar Chan
Foshan Rayven lighting Co.,LtdFoshan Rayven lighting Co.,Ltd
Foshan Rayven lighting Co.,Ltd
Caesar Chan85 views
Akash rajguru project report sem v by Akash Rajguru
Akash rajguru project report sem vAkash rajguru project report sem v
Akash rajguru project report sem v
Akash Rajguru705 views
DA 592 - Term Project Report - Berker Kozan Can Koklu by Can Köklü
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
Can Köklü346 views
Introduction to development with Django web framework by Sammy Fung
Introduction to development with Django web frameworkIntroduction to development with Django web framework
Introduction to development with Django web framework
Sammy Fung191 views
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ... by Edureka!
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!3.2K views
Django Article V0 by Udi Bauman
Django Article V0Django Article V0
Django Article V0
Udi Bauman2K views

More from Andreu Vallbona Plazas

Localhost to the internet by
Localhost to the internetLocalhost to the internet
Localhost to the internetAndreu Vallbona Plazas
314 views17 slides
Apsl attrs by
Apsl   attrsApsl   attrs
Apsl attrsAndreu Vallbona Plazas
60 views11 slides
Apsl pycharm + docker by
Apsl   pycharm + dockerApsl   pycharm + docker
Apsl pycharm + dockerAndreu Vallbona Plazas
62 views11 slides
Apsl testing by
Apsl   testingApsl   testing
Apsl testingAndreu Vallbona Plazas
65 views9 slides
Apsl translation manager by
Apsl   translation managerApsl   translation manager
Apsl translation managerAndreu Vallbona Plazas
45 views12 slides
Pytest - testing tips and useful plugins by
Pytest - testing tips and useful pluginsPytest - testing tips and useful plugins
Pytest - testing tips and useful pluginsAndreu Vallbona Plazas
159 views8 slides

Recently uploaded

Special_edition_innovator_2023.pdf by
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdfWillDavies22
14 views6 slides
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum... by
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...NUS-ISS
28 views35 slides
Voice Logger - Telephony Integration Solution at Aegis by
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at AegisNirmal Sharma
17 views1 slide
Attacking IoT Devices from a Web Perspective - Linux Day by
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day Simone Onofri
15 views68 slides
20231123_Camunda Meetup Vienna.pdf by
20231123_Camunda Meetup Vienna.pdf20231123_Camunda Meetup Vienna.pdf
20231123_Camunda Meetup Vienna.pdfPhactum Softwareentwicklung GmbH
23 views73 slides
The details of description: Techniques, tips, and tangents on alternative tex... by
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...BookNet Canada
110 views24 slides

Recently uploaded(20)

Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2214 views
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum... by NUS-ISS
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...
NUS-ISS28 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma17 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada110 views
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by Vadym Kazulkin
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
Vadym Kazulkin70 views
Web Dev - 1 PPT.pdf by gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet52 views
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV by Splunk
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV
Splunk86 views
handbook for web 3 adoption.pdf by Liveplex
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdf
Liveplex19 views
Transcript: The Details of Description Techniques tips and tangents on altern... by BookNet Canada
Transcript: The Details of Description Techniques tips and tangents on altern...Transcript: The Details of Description Techniques tips and tangents on altern...
Transcript: The Details of Description Techniques tips and tangents on altern...
BookNet Canada119 views
Spesifikasi Lengkap ASUS Vivobook Go 14 by Dot Semarang
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14
Dot Semarang35 views
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software91 views
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values

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