SlideShare a Scribd company logo
Unit Testing Workshop
It’s not that scary
2
BEFORE THE WORKSHOP
● Download a code editor if you don’t have one
○ https://code.visualstudio.com/, or
○ https://www.jetbrains.com/pycharm/download/
● git clone https://github.com/davified/unit-testing-workshop
Windows users:
● docker run -it -v C:pathtounit-testing-workshop:/code -p 8888:8888 unit-testing-workshop
● Run all commands in the README up until:
○ nosetests --with-watch
● Configure your IDE (we’ll do this together)
3
WHY TEST?
Complexity
+
Change
©ThoughtWorks 2019 Commercial in Confidence
4
Feedback
Time
Unit
tests
Jupyter
notebooks
IT’S ALL ABOUT FEEDBACK
5
TEST PYRAMID
Unit tests
ML metrics tests
Any other
automated tests
Manual tests
Automate
d
Time
required
How do we unit test?
7
ANATOMY OF A UNIT TEST
assert_that(do_something(input) => expected_output)
8
ANATOMY OF A UNIT TEST
assert_that(do_something(input) => expected_output)
1. Think about what your function is doing
2. Write the simplest possible test case to assert that
9
ANATOMY OF A UNIT TEST
def add(n1, n2):
return n1 + n2
self.assertEqual(2, add(1,1))
10
EXERCISE 1: UNIT TESTING A CALCULATOR
1. Code walkthrough: src/test_calculator.py and src/calculator.py
11
EXERCISE 1: UNIT TESTING A CALCULATOR
● unittest module (built-in python module)
● How to write a unit test
○ File name must start with Test____
○ Define a class that extends unittest.TestCase
○ Method name must start with test____(self)
● Assertions
○ self.assertEqual(expected, actual())
● How to run tests
○ nosetests --with-watch
12
EXERCISE 1: UNIT TESTING A CALCULATOR - YOUR TURN!
● Implement
○ subtract(a, b)
○ multiply(a, b)
○ divide(a, b)
● Challenge: write the tests first!
13
GOOD TESTING PRACTICES
● Do the simplest thing possible
● Red green refactor
● Descriptive test names
● Pair program (+ ping pong)
14
ANTI-PATTERNS / BAD TESTING HABITS
● Redundant tests
● Bad test names
● Empty tests (no assertions)
● Unspecific tests (e.g. Asserting that add(2, 3) isNotNone)
● Non-deterministic tests (causes flaky tests)
● Tautological tests (i.e. repeating implementation details in test cases)
15
EXERCISE 2: UNIT TESTING + REFACTORING A JUPYTER NOTEBOOK
notebooks/sklearn-nlp-pipeline-before-refactoring.ipynb
16
EXERCISE 2: UNIT TESTING + REFACTORING A JUPYTER NOTEBOOK
Instructions
● Read the notebook to get a sense of what it’s doing (it’s training a simple
NLP model for sentiment classification)
● Sit back and relax
17
● Readable notebooks
● Automated tests -- no more visual eyeballing of long notebooks
● Smaller units → Separation of concerns
● Unit tests == living documentation
● Unit tests == safety harness
● Partition complexity (esp. if you TDD)
● Lesser bugs in production
● Possibility of refactoring → Ability to evolve to adapt to changing needs
BENEFITS
18
NEXT STEPS
● Learn by doing
○ http://codewars.com
○ Start with 1 unit test in your project codebase
○ Explore unittest docs: https://docs.python.org/3/library/unittest.html
(they’re good)
● Resources
○ Unit Testing for Data Scientists - Hanna Torrence
○ Lots of tutorials on the internet

More Related Content

What's hot

Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with Python
MicroPyramid .
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
Adam Stephensen
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Google test training
Google test trainingGoogle test training
Google test training
Thierry Gayet
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
 
Junit
JunitJunit
Testing with JUnit 5 and Spring
Testing with JUnit 5 and SpringTesting with JUnit 5 and Spring
Testing with JUnit 5 and Spring
VMware Tanzu
 
Junit 4.0
Junit 4.0Junit 4.0
Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
Test Coverage
Test CoverageTest Coverage
Test Coverage
Hiroyuki Takahashi
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
Eddy Reyes
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
Humberto Marchezi
 
Java Programming
Java ProgrammingJava Programming
Java Programming
Elizabeth alexander
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
R. Sosa
 
Clean code
Clean codeClean code
Clean code
Bulat Shakirzyanov
 

What's hot (20)

Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with Python
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Google test training
Google test trainingGoogle test training
Google test training
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Junit
JunitJunit
Junit
 
Testing with JUnit 5 and Spring
Testing with JUnit 5 and SpringTesting with JUnit 5 and Spring
Testing with JUnit 5 and Spring
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Test Coverage
Test CoverageTest Coverage
Test Coverage
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
Testes Unitários usando TestNG
Testes Unitários usando TestNGTestes Unitários usando TestNG
Testes Unitários usando TestNG
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
Clean code
Clean codeClean code
Clean code
 

Similar to Workshop: Unit Testing in Python

Write unit test from scratch
Write unit test from scratchWrite unit test from scratch
Write unit test from scratch
Wen-Shih Chao
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
Peter Kofler
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
Xavi Hidalgo
 
Property-based testing an open-source compiler, pflua (FOSDEM 2015)
Property-based testing an open-source compiler, pflua (FOSDEM 2015)Property-based testing an open-source compiler, pflua (FOSDEM 2015)
Property-based testing an open-source compiler, pflua (FOSDEM 2015)
Igalia
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
Anatoliy Okhotnikov
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier
Christian Hujer
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
Brett Child
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
dn
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testing
malcolmt
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in Swift
Amey Tavkar
 
Tdd in swift
Tdd in swiftTdd in swift
Tdd in swift
Javal Nanda
 
Demise of test scripts rise of test ideas
Demise of test scripts rise of test ideasDemise of test scripts rise of test ideas
Demise of test scripts rise of test ideas
Richard Robinson
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
vilniusjug
 
Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y Gomega
Software Guru
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
Andrea Polci
 
Introduction to Continuous Delivery
Introduction to Continuous DeliveryIntroduction to Continuous Delivery
Introduction to Continuous Delivery
Giovanni Toraldo
 
GTAC 2015
GTAC 2015GTAC 2015
GTAC 2015
Dino Su
 
Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5
rtpaem
 

Similar to Workshop: Unit Testing in Python (20)

Write unit test from scratch
Write unit test from scratchWrite unit test from scratch
Write unit test from scratch
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Property-based testing an open-source compiler, pflua (FOSDEM 2015)
Property-based testing an open-source compiler, pflua (FOSDEM 2015)Property-based testing an open-source compiler, pflua (FOSDEM 2015)
Property-based testing an open-source compiler, pflua (FOSDEM 2015)
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
 
UPC Plone Testing Talk
UPC Plone Testing TalkUPC Plone Testing Talk
UPC Plone Testing Talk
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testing
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in Swift
 
Tdd in swift
Tdd in swiftTdd in swift
Tdd in swift
 
Demise of test scripts rise of test ideas
Demise of test scripts rise of test ideasDemise of test scripts rise of test ideas
Demise of test scripts rise of test ideas
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
 
Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y Gomega
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Introduction to Continuous Delivery
Introduction to Continuous DeliveryIntroduction to Continuous Delivery
Introduction to Continuous Delivery
 
Code Retreat
Code RetreatCode Retreat
Code Retreat
 
GTAC 2015
GTAC 2015GTAC 2015
GTAC 2015
 
Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5
 

Recently uploaded

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 

Recently uploaded (20)

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 

Workshop: Unit Testing in Python

  • 2. 2 BEFORE THE WORKSHOP ● Download a code editor if you don’t have one ○ https://code.visualstudio.com/, or ○ https://www.jetbrains.com/pycharm/download/ ● git clone https://github.com/davified/unit-testing-workshop Windows users: ● docker run -it -v C:pathtounit-testing-workshop:/code -p 8888:8888 unit-testing-workshop ● Run all commands in the README up until: ○ nosetests --with-watch ● Configure your IDE (we’ll do this together)
  • 4. ©ThoughtWorks 2019 Commercial in Confidence 4 Feedback Time Unit tests Jupyter notebooks IT’S ALL ABOUT FEEDBACK
  • 5. 5 TEST PYRAMID Unit tests ML metrics tests Any other automated tests Manual tests Automate d Time required
  • 6. How do we unit test?
  • 7. 7 ANATOMY OF A UNIT TEST assert_that(do_something(input) => expected_output)
  • 8. 8 ANATOMY OF A UNIT TEST assert_that(do_something(input) => expected_output) 1. Think about what your function is doing 2. Write the simplest possible test case to assert that
  • 9. 9 ANATOMY OF A UNIT TEST def add(n1, n2): return n1 + n2 self.assertEqual(2, add(1,1))
  • 10. 10 EXERCISE 1: UNIT TESTING A CALCULATOR 1. Code walkthrough: src/test_calculator.py and src/calculator.py
  • 11. 11 EXERCISE 1: UNIT TESTING A CALCULATOR ● unittest module (built-in python module) ● How to write a unit test ○ File name must start with Test____ ○ Define a class that extends unittest.TestCase ○ Method name must start with test____(self) ● Assertions ○ self.assertEqual(expected, actual()) ● How to run tests ○ nosetests --with-watch
  • 12. 12 EXERCISE 1: UNIT TESTING A CALCULATOR - YOUR TURN! ● Implement ○ subtract(a, b) ○ multiply(a, b) ○ divide(a, b) ● Challenge: write the tests first!
  • 13. 13 GOOD TESTING PRACTICES ● Do the simplest thing possible ● Red green refactor ● Descriptive test names ● Pair program (+ ping pong)
  • 14. 14 ANTI-PATTERNS / BAD TESTING HABITS ● Redundant tests ● Bad test names ● Empty tests (no assertions) ● Unspecific tests (e.g. Asserting that add(2, 3) isNotNone) ● Non-deterministic tests (causes flaky tests) ● Tautological tests (i.e. repeating implementation details in test cases)
  • 15. 15 EXERCISE 2: UNIT TESTING + REFACTORING A JUPYTER NOTEBOOK notebooks/sklearn-nlp-pipeline-before-refactoring.ipynb
  • 16. 16 EXERCISE 2: UNIT TESTING + REFACTORING A JUPYTER NOTEBOOK Instructions ● Read the notebook to get a sense of what it’s doing (it’s training a simple NLP model for sentiment classification) ● Sit back and relax
  • 17. 17 ● Readable notebooks ● Automated tests -- no more visual eyeballing of long notebooks ● Smaller units → Separation of concerns ● Unit tests == living documentation ● Unit tests == safety harness ● Partition complexity (esp. if you TDD) ● Lesser bugs in production ● Possibility of refactoring → Ability to evolve to adapt to changing needs BENEFITS
  • 18. 18 NEXT STEPS ● Learn by doing ○ http://codewars.com ○ Start with 1 unit test in your project codebase ○ Explore unittest docs: https://docs.python.org/3/library/unittest.html (they’re good) ● Resources ○ Unit Testing for Data Scientists - Hanna Torrence ○ Lots of tutorials on the internet

Editor's Notes

  1. Add caveat on TDD
  2. Enemy number 1 of software: complexity What adds to complexity? Long methods Cyclomatic complexity Etc. etc. Change We can’t refactor without unit tests Living documentation about our code Partition complexity Discover bugs early
  3. Jupyter notebooks hit a roadblock at some point Unit tests can scale linearly
  4. Let me share ThoughtWorks’ approach to machine learning
  5. What to test: our functions What not to test: methods from a library
  6. What to test: our functions What not to test: methods from a library
  7. The goal of this exercise is to get you to familiarize yourself with the syntax; and see that it’s really not that scary
  8. Demo for them. Delete add and go through this slide