SlideShare a Scribd company logo
Odoo's Test Framework - Learn Best Practices
A good test suite really is an
executable specification.
Test-driven development (TDD)
while not finished:
test = Test()
assert not test()
code.write()
assert test()
while test() and code.canBeBetter():
code.refactor()
Odoo’s test classes
● Inherit from unittest’s TestCase
● Manage a database cursor
● Performed on an Odoo database
TransactionCase
Each method is run
in its own
transaction.
The transaction
cursor is not
committed.
SavepointCase
All methods are run
in a single
transaction.
Each method is
guarded by a
savepoint (like a
sub-transaction).
setUpClass()
setUp()
test_method1()
tearDown()
setUp()
test_method2()
tearDown()
...
tearDownClass()
setUpClass()
setUp()
test_method1()
tearDown()
setUp()
test_method2()
tearDown()
...
tearDownClass()
HttpCase, HttpSavepointCase
● Launches
○ the HTTP server
○ a Chrome browser (headless)
● Open any URL
● Run some JavaScript in the Odoo client
Available on test instances
self.registry
self.cr
self.uid
self.env
Server-side form
● A Python implementation of a form
● Reproduces what the client-side form does
● Create or edit a record «just like a user
would»
Server-side form
move_form = Form(self.env['account.move'])
move_form.partner_id = partner
...
with move_form.invoice_line_ids.new() as line_form:
line_form.product_id = product
...
invoice = move_form.save()
Server-side form
with Form(invoice) as move_form:
move_form.partner_id = partner
...
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.product_uom_id = uom
...
Use addCleanup()
def setUp(self):
super().setUp()
self.registry = odoo.registry(get_db_name())
self.addCleanup(self.registry.reset_changes)
self.addCleanup(self.registry.clear_caches)
#: current transaction's cursor
self.cr = self.cursor()
self.addCleanup(self.cr.close)
#: :class:`~odoo.api.Environment` for the current test case
self.env = api.Environment(self.cr, odoo.SUPERUSER_ID, {})
self.addCleanup(self.env.reset)
Use unittest.mock
from datetime import date
from unittest.mock import patch
day = date(1979, 5, 4)
with patch.object(fields.Date, 'today', lambda *args, **kwargs: day):
...
assert fields.Date.today() == day
...
Maximize coverage
● Every corner case
● Different users
● Access errors
● Expected errors
Thank You

More Related Content

PPTX
Deploying & Scaling your Odoo Server
PPTX
Owl: The New Odoo UI Framework
PDF
Asynchronous JS in Odoo
PDF
Tools for Solving Performance Issues
PPTX
Security: Odoo Code Hardening
PDF
Impact of the New ORM on Your Modules
PPTX
Common Performance Pitfalls in Odoo apps
PDF
Tips on how to improve the performance of your custom modules for high volume...
Deploying & Scaling your Odoo Server
Owl: The New Odoo UI Framework
Asynchronous JS in Odoo
Tools for Solving Performance Issues
Security: Odoo Code Hardening
Impact of the New ORM on Your Modules
Common Performance Pitfalls in Odoo apps
Tips on how to improve the performance of your custom modules for high volume...

What's hot (20)

PDF
이벤트 기반 분산 시스템을 향한 여정
PDF
Odoo - Create themes for website
PDF
스프링 시큐리티 구조 이해
PDF
Odoo Online platform: architecture and challenges
PDF
New Framework - ORM
PDF
Load Testing - How to Stress Your Odoo with Locust
PDF
Improving the performance of Odoo deployments
PDF
중니어의 고뇌: 1인분 개발자, 다음을 찾아서
ODP
Presto
DOCX
Simple Odoo ERP auto scaling on AWS
PPTX
Bigquery와 airflow를 이용한 데이터 분석 시스템 구축 v1 나무기술(주) 최유석 20170912
PPTX
4. 대용량 아키텍쳐 설계 패턴
PDF
The Best (and Worst) of Django
PDF
우아한 모노리스
PPTX
An in Depth Journey into Odoo's ORM
PPTX
Airflow를 이용한 데이터 Workflow 관리
PDF
Mvcc in postgreSQL 권건우
PDF
엘라스틱서치 실무 가이드_202204.pdf
PDF
Service Worker Presentation
PDF
jQuery -Chapter 2 - Selectors and Events
이벤트 기반 분산 시스템을 향한 여정
Odoo - Create themes for website
스프링 시큐리티 구조 이해
Odoo Online platform: architecture and challenges
New Framework - ORM
Load Testing - How to Stress Your Odoo with Locust
Improving the performance of Odoo deployments
중니어의 고뇌: 1인분 개발자, 다음을 찾아서
Presto
Simple Odoo ERP auto scaling on AWS
Bigquery와 airflow를 이용한 데이터 분석 시스템 구축 v1 나무기술(주) 최유석 20170912
4. 대용량 아키텍쳐 설계 패턴
The Best (and Worst) of Django
우아한 모노리스
An in Depth Journey into Odoo's ORM
Airflow를 이용한 데이터 Workflow 관리
Mvcc in postgreSQL 권건우
엘라스틱서치 실무 가이드_202204.pdf
Service Worker Presentation
jQuery -Chapter 2 - Selectors and Events
Ad

Similar to Odoo's Test Framework - Learn Best Practices (20)

PDF
Record Automated Integration Tests through the UI
PPTX
An Introduction To Software Development - Test Driven Development
PDF
Klaxit - How to keep it clean, for years - Paris.RB 2020
PPTX
PyCon APAC - Django Test Driven Development
PPTX
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
PPTX
Test Driven Development (TDD) Preso 360|Flex 2010
PPT
XP through TDD
PPTX
An Introduction To Software Development - Test Driven Development, Part 1
PDF
TechOut Solutions, LLC - A Short Introduction to Our Services and Processes
PDF
Programmers slang
PDF
How to implement ruby on rails testing practices to build a successful web ap...
PPTX
Cypress report
PPTX
Refactoring Legacy Code - true story
PDF
Testing survival Guide
PDF
"Streamline Business Operations with End-to-End Odoo Implementation Services ...
DOCX
Naresh Chirra
PDF
2010-07-19_rails_tdd_week1
PDF
Test Automation and Innovation with Open Source Tools
PPTX
Creative Business Presentation depi 1.ptx
PDF
Odoo Development_ The Key to Automate and Run Your Business Processes more pr...
Record Automated Integration Tests through the UI
An Introduction To Software Development - Test Driven Development
Klaxit - How to keep it clean, for years - Paris.RB 2020
PyCon APAC - Django Test Driven Development
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) Preso 360|Flex 2010
XP through TDD
An Introduction To Software Development - Test Driven Development, Part 1
TechOut Solutions, LLC - A Short Introduction to Our Services and Processes
Programmers slang
How to implement ruby on rails testing practices to build a successful web ap...
Cypress report
Refactoring Legacy Code - true story
Testing survival Guide
"Streamline Business Operations with End-to-End Odoo Implementation Services ...
Naresh Chirra
2010-07-19_rails_tdd_week1
Test Automation and Innovation with Open Source Tools
Creative Business Presentation depi 1.ptx
Odoo Development_ The Key to Automate and Run Your Business Processes more pr...
Ad

More from Odoo (20)

PPTX
Timesheet Workshop: The Timesheet App People Love!
PPTX
Odoo 3D Product View with Google Model-Viewer
PPTX
Keynote - Vision & Strategy
PPTX
Opening Keynote - Unveilling Odoo 14
PDF
Extending Odoo with a Comprehensive Budgeting and Forecasting Capability
PDF
Managing Multi-channel Selling with Odoo
PPTX
Product Configurator: Advanced Use Case
PDF
Accounting Automation: How Much Money We Saved and How?
PPTX
Rock Your Logistics with Advanced Operations
PPTX
Transition from a cost to a flow-centric organization
PDF
Synchronization: The Supply Chain Response to Overcome the Crisis
PPTX
Running a University with Odoo
PPTX
Down Payments on Purchase Orders in Odoo
PPTX
Odoo Implementation in Phases - Success Story of a Retail Chain 3Sach food
PPTX
Migration from Salesforce to Odoo
PPTX
Preventing User Mistakes by Using Machine Learning
PPTX
Becoming an Odoo Expert: How to Prepare for the Certification
PPTX
Instant Printing of any Odoo Report or Shipping Label
PPTX
How Odoo helped an Organization Grow 3 Fold
PPTX
From Shopify to Odoo
Timesheet Workshop: The Timesheet App People Love!
Odoo 3D Product View with Google Model-Viewer
Keynote - Vision & Strategy
Opening Keynote - Unveilling Odoo 14
Extending Odoo with a Comprehensive Budgeting and Forecasting Capability
Managing Multi-channel Selling with Odoo
Product Configurator: Advanced Use Case
Accounting Automation: How Much Money We Saved and How?
Rock Your Logistics with Advanced Operations
Transition from a cost to a flow-centric organization
Synchronization: The Supply Chain Response to Overcome the Crisis
Running a University with Odoo
Down Payments on Purchase Orders in Odoo
Odoo Implementation in Phases - Success Story of a Retail Chain 3Sach food
Migration from Salesforce to Odoo
Preventing User Mistakes by Using Machine Learning
Becoming an Odoo Expert: How to Prepare for the Certification
Instant Printing of any Odoo Report or Shipping Label
How Odoo helped an Organization Grow 3 Fold
From Shopify to Odoo

Recently uploaded (20)

PPTX
2025 Product Deck V1.0.pptxCATALOGTCLCIA
PDF
Digital Marketing & E-commerce Certificate Glossary.pdf.................
PPT
Data mining for business intelligence ch04 sharda
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PDF
Nidhal Samdaie CV - International Business Consultant
PPTX
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PPTX
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
PPTX
Belch_12e_PPT_Ch18_Accessible_university.pptx
PDF
IFRS Notes in your pocket for study all the time
PDF
Unit 1 Cost Accounting - Cost sheet
PDF
How to Get Funding for Your Trucking Business
PPTX
New Microsoft PowerPoint Presentation - Copy.pptx
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
Roadmap Map-digital Banking feature MB,IB,AB
PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
PPTX
Amazon (Business Studies) management studies
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
2025 Product Deck V1.0.pptxCATALOGTCLCIA
Digital Marketing & E-commerce Certificate Glossary.pdf.................
Data mining for business intelligence ch04 sharda
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
unit 1 COST ACCOUNTING AND COST SHEET
Nidhal Samdaie CV - International Business Consultant
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
Ôn tập tiếng anh trong kinh doanh nâng cao
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
Belch_12e_PPT_Ch18_Accessible_university.pptx
IFRS Notes in your pocket for study all the time
Unit 1 Cost Accounting - Cost sheet
How to Get Funding for Your Trucking Business
New Microsoft PowerPoint Presentation - Copy.pptx
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
Roadmap Map-digital Banking feature MB,IB,AB
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
Amazon (Business Studies) management studies
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi

Odoo's Test Framework - Learn Best Practices

  • 2. A good test suite really is an executable specification.
  • 3. Test-driven development (TDD) while not finished: test = Test() assert not test() code.write() assert test() while test() and code.canBeBetter(): code.refactor()
  • 4. Odoo’s test classes ● Inherit from unittest’s TestCase ● Manage a database cursor ● Performed on an Odoo database
  • 5. TransactionCase Each method is run in its own transaction. The transaction cursor is not committed. SavepointCase All methods are run in a single transaction. Each method is guarded by a savepoint (like a sub-transaction). setUpClass() setUp() test_method1() tearDown() setUp() test_method2() tearDown() ... tearDownClass() setUpClass() setUp() test_method1() tearDown() setUp() test_method2() tearDown() ... tearDownClass()
  • 6. HttpCase, HttpSavepointCase ● Launches ○ the HTTP server ○ a Chrome browser (headless) ● Open any URL ● Run some JavaScript in the Odoo client
  • 7. Available on test instances self.registry self.cr self.uid self.env
  • 8. Server-side form ● A Python implementation of a form ● Reproduces what the client-side form does ● Create or edit a record «just like a user would»
  • 9. Server-side form move_form = Form(self.env['account.move']) move_form.partner_id = partner ... with move_form.invoice_line_ids.new() as line_form: line_form.product_id = product ... invoice = move_form.save()
  • 10. Server-side form with Form(invoice) as move_form: move_form.partner_id = partner ... with move_form.invoice_line_ids.edit(0) as line_form: line_form.product_uom_id = uom ...
  • 11. Use addCleanup() def setUp(self): super().setUp() self.registry = odoo.registry(get_db_name()) self.addCleanup(self.registry.reset_changes) self.addCleanup(self.registry.clear_caches) #: current transaction's cursor self.cr = self.cursor() self.addCleanup(self.cr.close) #: :class:`~odoo.api.Environment` for the current test case self.env = api.Environment(self.cr, odoo.SUPERUSER_ID, {}) self.addCleanup(self.env.reset)
  • 12. Use unittest.mock from datetime import date from unittest.mock import patch day = date(1979, 5, 4) with patch.object(fields.Date, 'today', lambda *args, **kwargs: day): ... assert fields.Date.today() == day ...
  • 13. Maximize coverage ● Every corner case ● Different users ● Access errors ● Expected errors

Editor's Notes

  • #2: Welcome developers! This talk gives advice on best testing in Python. I am Raphael Collet, and I work as a developer. My job is to maintain and improve the Odoo server-side framework.
  • #3: Welcome developers! This talk gives advice on best testing in Python. I am Raphael Collet, and I work as a developer. My job is to maintain and improve the Odoo server-side framework.
  • #10: Since Odoo 12.0
  • #11: This invokes the form to create an invoice.
  • #13: Simpler to use than tearDown(). Also works inside test methods. There is addClassCleanup() for setUpClass() methods.
  • #14: Flexible enough for most fixtures. Automatically removed upon tearDown() Hand-written mockups often leave the system in a broken state.