KEMBAR78
Workshop: Unit Testing in Python | PPTX
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

Workshop: Unit Testing in Python

  • 1.
  • 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)
  • 3.
  • 4.
    ©ThoughtWorks 2019 Commercialin Confidence 4 Feedback Time Unit tests Jupyter notebooks IT’S ALL ABOUT FEEDBACK
  • 5.
    5 TEST PYRAMID Unit tests MLmetrics tests Any other automated tests Manual tests Automate d Time required
  • 6.
    How do weunit test?
  • 7.
    7 ANATOMY OF AUNIT TEST assert_that(do_something(input) => expected_output)
  • 8.
    8 ANATOMY OF AUNIT 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 AUNIT TEST def add(n1, n2): return n1 + n2 self.assertEqual(2, add(1,1))
  • 10.
    10 EXERCISE 1: UNITTESTING A CALCULATOR 1. Code walkthrough: src/test_calculator.py and src/calculator.py
  • 11.
    11 EXERCISE 1: UNITTESTING 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: UNITTESTING 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 / BADTESTING 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: UNITTESTING + REFACTORING A JUPYTER NOTEBOOK notebooks/sklearn-nlp-pipeline-before-refactoring.ipynb
  • 16.
    16 EXERCISE 2: UNITTESTING + 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 ● Learnby 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

  • #2 Add caveat on TDD
  • #4 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
  • #5 Jupyter notebooks hit a roadblock at some point Unit tests can scale linearly
  • #7 Let me share ThoughtWorks’ approach to machine learning
  • #9 What to test: our functions What not to test: methods from a library
  • #10 What to test: our functions What not to test: methods from a library
  • #11 The goal of this exercise is to get you to familiarize yourself with the syntax; and see that it’s really not that scary
  • #12 Demo for them. Delete add and go through this slide