SlideShare a Scribd company logo
1 of 24
Download to read offline
Multiply your Testing
Effectiveness with
Parameterized Testing
Brian Okken
The Immense Value of
Automated Tests
and
How to Avoid Writing Them
Alternate title
Brian Okken
Brian Okken
weekly Python podcasts A book
I work here
new meetup there, Python PDX West Oct 8, 6 pm
Python-PDX-West
Outline
• A development workflow
• which includes a build pipeline
• which includes tests
• that I don’t want to spend too much time writing
• so most of my test cases use parametrization*
*one of many techniques I use to avoid writing tests
Target Workflow
Time
main
dev / fix / feature
branches
Merge Request /
Pipeline Runs
Drawing elements: Vincent Driessen
License: Creative Commons
Merge Request /
Pipeline Runs
• Branch off main
• Solo or collaborating
• Tests and code merge together
• Pipeline does magic
Developer Workflow
• Write some code & some tests.
• Commit code regularly
• Merge Request / Pull Request
• Pipeline does most of the work, build, test, etc.
• Reviewers get notified.
• Reviewers think my code is awesome & accept it.
• Merge finishes
• Fist bumps, high fives, etc.
• Repeat
After merge, I know
• I didn't break anything that used to work.
• New features are tested with new tests.
• Future changes won’t break current features.
• Team understands code and tests.
• The code is ready for users.
• I can refactor my code if I'm not proud of it and
know the tests will make sure everything is ok.
Reviewer knows,
before the review
✓ Static analysis
✓ Style guide checks
✓ Code coverage has not dropped.
✓ Tests all pass
✓ Legacy functionality working.
✓ New tests pass.
Reviewer Focus
• Just the code + test for this feature.
• Do I understand the code and the tests?
• Enough to maintain it if the original dev is on vacation?
• Are the tests sufficient for the new functionality?
Team Lead / Manager View
• Awesome code keeps popping out
• The tests have our backs.
• We’re moving fast.
• Big refactoring/rewrites are low risk.
• I can understand the tests.
• Maybe even write some tests myself.
Tests in a Pipeline
• fail fast
• negative feedback as fast as possible
• feeds into a deploy stage, maybe
smoke
tests
longer
running
tests
quick but
thorough
new tests
static
analysis
13
Tests to support this
• Customer focused
• Developer focused
• Feature / functionality focused
• Risk focused
• Complete but not crazy complete
• Have to be readable, fast to write, easy to maintain
Parametrization
• Many test cases with one test function.
• pytest has a few strategies for this.
• function parametrization
• fixture parametrization
• a hook function: pytest_generate_tests()
17
cards
$ cards
ID owner done summary
---- ------- ------ ————
$ cards add prepare for talk
$ cards add give talk
$ cards
ID owner done summary
---- ------- ------ ----------------
1 prepare for talk
2 give talk
$ cards update -o okken 1
$ cards update -o okken 2
$ cards finish 1
$ cards
ID owner done summary
---- ------- ------ ----------------
1 okken x prepare for talk
2 okken give talk
a test
import cards
from cards import Card
def test_add(tmp_path):
cards.set_db_path(tmp_path)
cards.connect()
a_card = Card('first task', 'brian', False)
id = cards.add_card(a_card)
c2 = cards.get_card(id)
cards.disconnect()
assert a_card == c2
push setup/teardown
into fixtures
@pytest.fixture(scope='session')
def db(tmp_path_factory):
d = tmp_path_factory.mktemp('cards_db')
cards.set_db_path(d)
cards.connect()
yield
cards.disconnect()
@pytest.fixture(scope='function')
def empty_db(db):
cards.delete_all()
def test_add(empty_db):
a_card = Card('first task', 'brian')
id = cards.add_card(a_card)
c2 = cards.get_card(id)
assert a_card == c2
so we can focus on this test
def test_add(empty_db):
a_card = Card('first task', 'brian', False)
id = cards.add_card(a_card)
c2 = cards.get_card(id)
assert a_card == c2
so we can focus on this test
def test_add(empty_db):
a_card = Card('first task', 'brian', False)
id = cards.add_card(a_card)
c2 = cards.get_card(id)
assert a_card == c2
@dataclass
class Card:
summary: str = None
owner: str = None
done: bool = None
id: int = field(default=None, compare=False)
But what about all
the other kinds of cards?
Parametrization !!!
@pytest.mark.parametrize('a_card', [
Card('first task', 'brian', False),
Card(),
Card(summary='do something'),
Card(owner='brian'),
Card(done=True)],ids=repr)
def test_add(empty_db, a_card):
id = cards.add_card(a_card)
c2 = cards.get_card(id)
assert a_card == c2
function parametrization
@pytest.fixture(params=[
Card('first task', 'brian', False),
Card(),
Card(summary='do something'),
Card(owner='brian'),
Card(done=True)], ids=repr)
def a_card(request):
return request.param
def test_add(empty_db, a_card):
id = cards.add_card(a_card)
c2 = cards.get_card(id)
assert a_card == c2
fixture parametrization
def pytest_generate_tests(metafunc):
if "a_card" in metafunc.fixturenames:
metafunc.parametrize("a_card", [
Card('first task', 'brian', False),
Card(),
Card(summary='do something'),
Card(owner='brian'),
Card(done=True)], ids = repr)
def test_add(empty_db, a_card):
id = cards.add_card(a_card)
c2 = cards.get_card(id)
assert a_card == c2
pytest_generate_tests
Thank You
• twitter: @brianokken
• book: Python Testing with pytest
• pragprog.com/book/bopytest/python-testing-with-pytest
• also: pytestbook.com
• podcasts
• testandcode.com
• pythonbytes.fm
• https://www.me
• meetup
• meetup.com/Python-PDX-West/
• First one: Tuesday, Oct 8, Hillsboro

More Related Content

What's hot

Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005
dflexer
 
Linux kernel-rootkit-dev - Wonokaerun
Linux kernel-rootkit-dev - WonokaerunLinux kernel-rootkit-dev - Wonokaerun
Linux kernel-rootkit-dev - Wonokaerun
idsecconf
 

What's hot (20)

YOW2020 Linux Systems Performance
YOW2020 Linux Systems PerformanceYOW2020 Linux Systems Performance
YOW2020 Linux Systems Performance
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
 
Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Linux kernel-rootkit-dev - Wonokaerun
Linux kernel-rootkit-dev - WonokaerunLinux kernel-rootkit-dev - Wonokaerun
Linux kernel-rootkit-dev - Wonokaerun
 
LSFMM 2019 BPF Observability
LSFMM 2019 BPF ObservabilityLSFMM 2019 BPF Observability
LSFMM 2019 BPF Observability
 
LPC2019 BPF Tracing Tools
LPC2019 BPF Tracing ToolsLPC2019 BPF Tracing Tools
LPC2019 BPF Tracing Tools
 
Performance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedPerformance Wins with BPF: Getting Started
Performance Wins with BPF: Getting Started
 
Kernel development
Kernel developmentKernel development
Kernel development
 
eBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to Userspace
 
bcc/BPF tools - Strategy, current tools, future challenges
bcc/BPF tools - Strategy, current tools, future challengesbcc/BPF tools - Strategy, current tools, future challenges
bcc/BPF tools - Strategy, current tools, future challenges
 
Troubleshooting Linux Kernel Modules And Device Drivers
Troubleshooting Linux Kernel Modules And Device DriversTroubleshooting Linux Kernel Modules And Device Drivers
Troubleshooting Linux Kernel Modules And Device Drivers
 
Linux Performance Tools 2014
Linux Performance Tools 2014Linux Performance Tools 2014
Linux Performance Tools 2014
 
Spying on the Linux kernel for fun and profit
Spying on the Linux kernel for fun and profitSpying on the Linux kernel for fun and profit
Spying on the Linux kernel for fun and profit
 
Security Monitoring with eBPF
Security Monitoring with eBPFSecurity Monitoring with eBPF
Security Monitoring with eBPF
 
Trace kernel code tips
Trace kernel code tipsTrace kernel code tips
Trace kernel code tips
 
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of Software
 
re:Invent 2019 BPF Performance Analysis at Netflix
re:Invent 2019 BPF Performance Analysis at Netflixre:Invent 2019 BPF Performance Analysis at Netflix
re:Invent 2019 BPF Performance Analysis at Netflix
 
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPFLinux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPF
 
Low Overhead System Tracing with eBPF
Low Overhead System Tracing with eBPFLow Overhead System Tracing with eBPF
Low Overhead System Tracing with eBPF
 

Similar to Multiply your Testing Effectiveness with Parameterized Testing, v1

Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World Domination
cPanel
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Ortus Solutions, Corp
 

Similar to Multiply your Testing Effectiveness with Parameterized Testing, v1 (20)

Static Code Analysis PHP[tek] 2023
Static Code Analysis PHP[tek] 2023Static Code Analysis PHP[tek] 2023
Static Code Analysis PHP[tek] 2023
 
Continuous feature-development
Continuous feature-developmentContinuous feature-development
Continuous feature-development
 
C++ Testing Techniques Tips and Tricks - C++ London
C++ Testing Techniques Tips and Tricks - C++ LondonC++ Testing Techniques Tips and Tricks - C++ London
C++ Testing Techniques Tips and Tricks - C++ London
 
Workshop quality assurance for php projects - phpdublin
Workshop quality assurance for php projects - phpdublinWorkshop quality assurance for php projects - phpdublin
Workshop quality assurance for php projects - phpdublin
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
Continuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIContinuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CI
 
Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World Domination
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Machine learning in PHP
Machine learning in PHPMachine learning in PHP
Machine learning in PHP
 
Machine learning in php singapore
Machine learning in php   singaporeMachine learning in php   singapore
Machine learning in php singapore
 
What is this agile thing anyway
What is this agile thing anywayWhat is this agile thing anyway
What is this agile thing anyway
 
Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 
Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.
 
Tdd
TddTdd
Tdd
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and Challenges
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

Multiply your Testing Effectiveness with Parameterized Testing, v1

  • 1. Multiply your Testing Effectiveness with Parameterized Testing Brian Okken
  • 2. The Immense Value of Automated Tests and How to Avoid Writing Them Alternate title Brian Okken
  • 3. Brian Okken weekly Python podcasts A book I work here new meetup there, Python PDX West Oct 8, 6 pm Python-PDX-West
  • 4. Outline • A development workflow • which includes a build pipeline • which includes tests • that I don’t want to spend too much time writing • so most of my test cases use parametrization* *one of many techniques I use to avoid writing tests
  • 5. Target Workflow Time main dev / fix / feature branches Merge Request / Pipeline Runs Drawing elements: Vincent Driessen License: Creative Commons Merge Request / Pipeline Runs • Branch off main • Solo or collaborating • Tests and code merge together • Pipeline does magic
  • 6. Developer Workflow • Write some code & some tests. • Commit code regularly • Merge Request / Pull Request • Pipeline does most of the work, build, test, etc. • Reviewers get notified. • Reviewers think my code is awesome & accept it. • Merge finishes • Fist bumps, high fives, etc. • Repeat
  • 7. After merge, I know • I didn't break anything that used to work. • New features are tested with new tests. • Future changes won’t break current features. • Team understands code and tests. • The code is ready for users. • I can refactor my code if I'm not proud of it and know the tests will make sure everything is ok.
  • 8. Reviewer knows, before the review ✓ Static analysis ✓ Style guide checks ✓ Code coverage has not dropped. ✓ Tests all pass ✓ Legacy functionality working. ✓ New tests pass.
  • 9. Reviewer Focus • Just the code + test for this feature. • Do I understand the code and the tests? • Enough to maintain it if the original dev is on vacation? • Are the tests sufficient for the new functionality?
  • 10. Team Lead / Manager View • Awesome code keeps popping out • The tests have our backs. • We’re moving fast. • Big refactoring/rewrites are low risk. • I can understand the tests. • Maybe even write some tests myself.
  • 11. Tests in a Pipeline • fail fast • negative feedback as fast as possible • feeds into a deploy stage, maybe smoke tests longer running tests quick but thorough new tests static analysis 13
  • 12. Tests to support this • Customer focused • Developer focused • Feature / functionality focused • Risk focused • Complete but not crazy complete • Have to be readable, fast to write, easy to maintain
  • 13. Parametrization • Many test cases with one test function. • pytest has a few strategies for this. • function parametrization • fixture parametrization • a hook function: pytest_generate_tests() 17
  • 14. cards $ cards ID owner done summary ---- ------- ------ ———— $ cards add prepare for talk $ cards add give talk $ cards ID owner done summary ---- ------- ------ ---------------- 1 prepare for talk 2 give talk $ cards update -o okken 1 $ cards update -o okken 2 $ cards finish 1 $ cards ID owner done summary ---- ------- ------ ---------------- 1 okken x prepare for talk 2 okken give talk
  • 15. a test import cards from cards import Card def test_add(tmp_path): cards.set_db_path(tmp_path) cards.connect() a_card = Card('first task', 'brian', False) id = cards.add_card(a_card) c2 = cards.get_card(id) cards.disconnect() assert a_card == c2
  • 16. push setup/teardown into fixtures @pytest.fixture(scope='session') def db(tmp_path_factory): d = tmp_path_factory.mktemp('cards_db') cards.set_db_path(d) cards.connect() yield cards.disconnect() @pytest.fixture(scope='function') def empty_db(db): cards.delete_all() def test_add(empty_db): a_card = Card('first task', 'brian') id = cards.add_card(a_card) c2 = cards.get_card(id) assert a_card == c2
  • 17. so we can focus on this test def test_add(empty_db): a_card = Card('first task', 'brian', False) id = cards.add_card(a_card) c2 = cards.get_card(id) assert a_card == c2
  • 18. so we can focus on this test def test_add(empty_db): a_card = Card('first task', 'brian', False) id = cards.add_card(a_card) c2 = cards.get_card(id) assert a_card == c2 @dataclass class Card: summary: str = None owner: str = None done: bool = None id: int = field(default=None, compare=False) But what about all the other kinds of cards?
  • 20. @pytest.mark.parametrize('a_card', [ Card('first task', 'brian', False), Card(), Card(summary='do something'), Card(owner='brian'), Card(done=True)],ids=repr) def test_add(empty_db, a_card): id = cards.add_card(a_card) c2 = cards.get_card(id) assert a_card == c2 function parametrization
  • 21. @pytest.fixture(params=[ Card('first task', 'brian', False), Card(), Card(summary='do something'), Card(owner='brian'), Card(done=True)], ids=repr) def a_card(request): return request.param def test_add(empty_db, a_card): id = cards.add_card(a_card) c2 = cards.get_card(id) assert a_card == c2 fixture parametrization
  • 22. def pytest_generate_tests(metafunc): if "a_card" in metafunc.fixturenames: metafunc.parametrize("a_card", [ Card('first task', 'brian', False), Card(), Card(summary='do something'), Card(owner='brian'), Card(done=True)], ids = repr) def test_add(empty_db, a_card): id = cards.add_card(a_card) c2 = cards.get_card(id) assert a_card == c2 pytest_generate_tests
  • 23.
  • 24. Thank You • twitter: @brianokken • book: Python Testing with pytest • pragprog.com/book/bopytest/python-testing-with-pytest • also: pytestbook.com • podcasts • testandcode.com • pythonbytes.fm • https://www.me • meetup • meetup.com/Python-PDX-West/ • First one: Tuesday, Oct 8, Hillsboro