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

Inverting The Testing Pyramid
Inverting The Testing PyramidInverting The Testing Pyramid
Inverting The Testing Pyramid
Naresh Jain
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
Maveryx
 
Role Of Qa And Testing In Agile 1225221397167302 8
Role Of Qa And Testing In Agile 1225221397167302 8Role Of Qa And Testing In Agile 1225221397167302 8
Role Of Qa And Testing In Agile 1225221397167302 8
a34sharm
 

What's hot (20)

Agile testing
Agile testingAgile testing
Agile testing
 
Agile Qa Framework Jacky Wu
Agile Qa Framework Jacky WuAgile Qa Framework Jacky Wu
Agile Qa Framework Jacky Wu
 
Inverting The Testing Pyramid
Inverting The Testing PyramidInverting The Testing Pyramid
Inverting The Testing Pyramid
 
boundary value analysis and equivalence partition
boundary value analysis and equivalence partitionboundary value analysis and equivalence partition
boundary value analysis and equivalence partition
 
Agile Testing: The Role Of The Agile Tester
Agile Testing: The Role Of The Agile TesterAgile Testing: The Role Of The Agile Tester
Agile Testing: The Role Of The Agile Tester
 
Exploratory testing workshop
Exploratory testing workshopExploratory testing workshop
Exploratory testing workshop
 
CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
CI/CD with Jenkins and Docker - DevOps Meetup Day ThailandCI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
 
Getting Started with Kubernetes
Getting Started with Kubernetes Getting Started with Kubernetes
Getting Started with Kubernetes
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
 
NMAP
NMAPNMAP
NMAP
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
ISTQB Foundation Level Basic
ISTQB Foundation Level BasicISTQB Foundation Level Basic
ISTQB Foundation Level Basic
 
Effective Software Test Case Design Approach
Effective Software Test Case Design ApproachEffective Software Test Case Design Approach
Effective Software Test Case Design Approach
 
Agile QA Process
Agile QA ProcessAgile QA Process
Agile QA Process
 
QA Best Practices in Agile World_new
QA Best Practices in Agile World_newQA Best Practices in Agile World_new
QA Best Practices in Agile World_new
 
Agile testing principles and practices - Anil Karade
Agile testing principles and practices - Anil KaradeAgile testing principles and practices - Anil Karade
Agile testing principles and practices - Anil Karade
 
Code Quality in Ruby and Java
Code Quality in Ruby and JavaCode Quality in Ruby and Java
Code Quality in Ruby and Java
 
Role Of Qa And Testing In Agile 1225221397167302 8
Role Of Qa And Testing In Agile 1225221397167302 8Role Of Qa And Testing In Agile 1225221397167302 8
Role Of Qa And Testing In Agile 1225221397167302 8
 
Live Response Collection Overview
Live Response Collection OverviewLive Response Collection Overview
Live Response Collection Overview
 
External to DA, the OS X Way
External to DA, the OS X WayExternal to DA, the OS X Way
External to DA, the OS X Way
 

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

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
Can Köklü
 
Django Article V0
Django Article V0Django Article V0
Django Article V0
Udi 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

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

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