SlideShare a Scribd company logo
1 of 8
Download to read offline
pytest
testing tips and useful plugins
whoami
● Andreu Vallbona @avallbona
● Bachelor degree in computer science
● Professional programmer since 1995
● Web developer at APSL, Mallorca, Spain
tips
● test database in localhost and/or in memory
● be careful with the Django signals
● mock external services (pytest-mock)
● take advantage of concurrent execution (pytest-xdist)
● reuse database from last run (pytest --reuse-db)
tips
● do not overly sophisticate the tests
● use what are, IMHO, the two key features of pytest:
○ fixtures: very easy way to inject the dependencies
(mostly objects) you need in order to recreate your
pre-test scenario
○ parametrize: allow us to execute the same test with
diferents parameters sets (use cases)
pytest-django
provide us several fixtures, among others, 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
model-mommy (like factory_boy but easier)
create fixtures based on django models very easily
● field values are generated automatically
● content random of the fields but can be specified individually
● You can create objects:
○ only 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 set of objects related to each other
● sequence fields can be defined
● you can create data generators for a given type of field
“Talk is cheap. Show me the code.”
― Linus Torvalds
a more in depth insight at:
http://talks.apsl.io/testing-pycones-2017/

More Related Content

What's hot

Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalksSelenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Lohika_Odessa_TechTalks
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 

What's hot (20)

unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutes
 
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalksSelenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
 
Intro To Spring Python
Intro To Spring PythonIntro To Spring Python
Intro To Spring Python
 
Python unittest
Python unittestPython unittest
Python unittest
 
Implementing a decorator for thread synchronisation.
Implementing a decorator for thread synchronisation.Implementing a decorator for thread synchronisation.
Implementing a decorator for thread synchronisation.
 
Can you upgrade to Puppet 4.x?
Can you upgrade to Puppet 4.x?Can you upgrade to Puppet 4.x?
Can you upgrade to Puppet 4.x?
 
Voxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with JavassistVoxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with Javassist
 
Puppet modules: An Holistic Approach
Puppet modules: An Holistic ApproachPuppet modules: An Holistic Approach
Puppet modules: An Holistic Approach
 
Pytest: escreva menos, teste mais
Pytest: escreva menos, teste maisPytest: escreva menos, teste mais
Pytest: escreva menos, teste mais
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With Javassist
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Oredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java AgentsOredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java Agents
 
How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with Javassist
 
Test your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practiceTest your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practice
 
Dynamic Python
Dynamic PythonDynamic Python
Dynamic Python
 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio module
 
Puppet Systems Infrastructure Construction Kit
Puppet Systems Infrastructure Construction KitPuppet Systems Infrastructure Construction Kit
Puppet Systems Infrastructure Construction Kit
 
Puppet Continuous Integration with PE and GitLab
Puppet Continuous Integration with PE and GitLabPuppet Continuous Integration with PE and GitLab
Puppet Continuous Integration with PE and GitLab
 

Similar to Pytest - testing tips and useful plugins

Елена Панина - Drupal performance testing. Тестирование производительности, м...
Елена Панина - Drupal performance testing. Тестирование производительности, м...Елена Панина - Drupal performance testing. Тестирование производительности, м...
Елена Панина - Drupal performance testing. Тестирование производительности, м...
LEDC 2016
 
Creating a mature puppet system
Creating a mature puppet systemCreating a mature puppet system
Creating a mature puppet system
rkhatibi
 
Next Generation Automation in Ruckus Wireless
Next Generation Automation in Ruckus WirelessNext Generation Automation in Ruckus Wireless
Next Generation Automation in Ruckus Wireless
David Ko
 

Similar to Pytest - testing tips and useful plugins (20)

Write unit test from scratch
Write unit test from scratchWrite unit test from scratch
Write unit test from scratch
 
HKG18-TR12 - LAVA for LITE Platforms and Tests
HKG18-TR12 - LAVA for LITE Platforms and TestsHKG18-TR12 - LAVA for LITE Platforms and Tests
HKG18-TR12 - LAVA for LITE Platforms and Tests
 
Testing Django APIs
Testing Django APIsTesting Django APIs
Testing Django APIs
 
Load testing in Zonky with Gatling
Load testing in Zonky with GatlingLoad testing in Zonky with Gatling
Load testing in Zonky with Gatling
 
Advanced API Debugging
Advanced API DebuggingAdvanced API Debugging
Advanced API Debugging
 
All of the thing about Postman
All of the thing about PostmanAll of the thing about Postman
All of the thing about Postman
 
Moodle Development Best Pracitces
Moodle Development Best PracitcesMoodle Development Best Pracitces
Moodle Development Best Pracitces
 
POUG2019 - Test your PL/SQL - your database will love you
POUG2019 - Test your PL/SQL - your database will love youPOUG2019 - Test your PL/SQL - your database will love you
POUG2019 - Test your PL/SQL - your database will love you
 
Bgoug 2019.11 test your pl sql - not your patience
Bgoug 2019.11   test your pl sql - not your patienceBgoug 2019.11   test your pl sql - not your patience
Bgoug 2019.11 test your pl sql - not your patience
 
AnalyticOps - Chicago PAW 2016
AnalyticOps - Chicago PAW 2016AnalyticOps - Chicago PAW 2016
AnalyticOps - Chicago PAW 2016
 
Елена Панина - Drupal performance testing. Тестирование производительности, м...
Елена Панина - Drupal performance testing. Тестирование производительности, м...Елена Панина - Drupal performance testing. Тестирование производительности, м...
Елена Панина - Drupal performance testing. Тестирование производительности, м...
 
Creating a Mature Puppet System
Creating a Mature Puppet SystemCreating a Mature Puppet System
Creating a Mature Puppet System
 
Creating a mature puppet system
Creating a mature puppet systemCreating a mature puppet system
Creating a mature puppet system
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
 
HiPEAC 2019 Tutorial - Maestro RTOS
HiPEAC 2019 Tutorial - Maestro RTOSHiPEAC 2019 Tutorial - Maestro RTOS
HiPEAC 2019 Tutorial - Maestro RTOS
 
EuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears TrainingEuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears Training
 
Next Generation Automation in Ruckus Wireless
Next Generation Automation in Ruckus WirelessNext Generation Automation in Ruckus Wireless
Next Generation Automation in Ruckus Wireless
 
Automation tools: making things go... (March 2019)
Automation tools: making things go... (March 2019)Automation tools: making things go... (March 2019)
Automation tools: making things go... (March 2019)
 
ML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning Infrastructure
ML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning InfrastructureML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning Infrastructure
ML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning Infrastructure
 
Eko10 Workshop Opensource Database Auditing
Eko10  Workshop Opensource Database AuditingEko10  Workshop Opensource Database Auditing
Eko10 Workshop Opensource Database Auditing
 

More from Andreu Vallbona Plazas

More from Andreu Vallbona Plazas (9)

Localhost to the internet
Localhost to the internetLocalhost to the internet
Localhost to the internet
 
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
 
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
 
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
 
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
 

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
 

Recently uploaded (20)

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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
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
 
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...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
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...
 

Pytest - testing tips and useful plugins

  • 1. pytest testing tips and useful plugins
  • 2. whoami ● Andreu Vallbona @avallbona ● Bachelor degree in computer science ● Professional programmer since 1995 ● Web developer at APSL, Mallorca, Spain
  • 3. tips ● test database in localhost and/or in memory ● be careful with the Django signals ● mock external services (pytest-mock) ● take advantage of concurrent execution (pytest-xdist) ● reuse database from last run (pytest --reuse-db)
  • 4. tips ● do not overly sophisticate the tests ● use what are, IMHO, the two key features of pytest: ○ fixtures: very easy way to inject the dependencies (mostly objects) you need in order to recreate your pre-test scenario ○ parametrize: allow us to execute the same test with diferents parameters sets (use cases)
  • 5. pytest-django provide us several fixtures, among others, 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
  • 6. model-mommy (like factory_boy but easier) create fixtures based on django models very easily ● field values are generated automatically ● content random of the fields but can be specified individually ● You can create objects: ○ only 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 set of objects related to each other ● sequence fields can be defined ● you can create data generators for a given type of field
  • 7. “Talk is cheap. Show me the code.” ― Linus Torvalds
  • 8. a more in depth insight at: http://talks.apsl.io/testing-pycones-2017/