Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adopt-a-pytest

 Adopt-a-pytest

pytest is a testing framework that makes writing and running Python tests simpler. Adopting new tooling in a large system is often a burden. How can you introduce pytest gradually with minimal pain?

With its simplified syntax, powerful fixture behaviors, detailed test reports, and plugin-based architecture, pytest has a lot to offer. Whether you're new to Python unit testing or you've been using unittest for a while, pytest may be something to consider. It's not too hard to get up and running with pytest on a fresh project, but how can you retrofit an existing project without having to refactor the world all at once?

9da94e6b6107563a68d185a02137a32e?s=128

Dane Hillard

July 27, 2019
Tweet

Transcript

  1. @easyaspython PyOo !1

  2. @easyaspython Adopt-a-pytest PyOhio 2019 Dane Hillard Lead Web Application Developer

    !2
  3. @easyaspython About me Reasons to pytest How to pytest More

    reasons to pytest !3
  4. @easyaspython What’s a Dane? !4

  5. @easyaspython ! Developing in Python (and Django) for ~7 years

    ! Web app development for ~4 years ! I do hobbies ◦ Lifestyle photographer ◦ Musician ◦ Epicurean ◦ Competitive ballroom dancer ◦ Blogger ◦ Author !5
  6. @easyaspython Practices of the Python Pro ! bit.ly/the-python-pro ! Gentle

    intro to software design practices !6
  7. @easyaspython !7

  8. @easyaspython !8

  9. @easyaspython !9 ! 50+ apps ! 900+ modules ! 300+

    test modules ! 2K+ tests Before: Manual, occasional, often broken Now: Automated*, every commit, not broken
  10. @easyaspython What’s a pytest? !10

  11. @easyaspython Simpler syntax !11

  12. @easyaspython !12

  13. @easyaspython !13

  14. @easyaspython !14

  15. @easyaspython !15

  16. @easyaspython Fixtures !16

  17. @easyaspython !17

  18. @easyaspython !18

  19. @easyaspython !19

  20. @easyaspython !20

  21. @easyaspython Marks, patterns, filtering !21

  22. @easyaspython !22

  23. @easyaspython !23

  24. @easyaspython !24

  25. @easyaspython !25

  26. @easyaspython Durations !26

  27. @easyaspython !27

  28. @easyaspython Parallels for most unittest stuff !28

  29. @easyaspython !29

  30. @easyaspython pytest is lovely !30

  31. @easyaspython Retrofitting is hard !31

  32. @easyaspython Step 0: Create a baseline !32

  33. @easyaspython !33

  34. @easyaspython Step 1: Install pytest !34

  35. @easyaspython !35

  36. @easyaspython Step 1a: Install pytest-cov !36

  37. @easyaspython Step 2: Run pytest !37

  38. @easyaspython !38

  39. @easyaspython !39

  40. @easyaspython !40

  41. @easyaspython !41

  42. @easyaspython Step 3: Fix test collection !42

  43. @easyaspython Test discovery !43 Convention unittest pytest Ⓜ test*.py ✅

    Ⓜ test_*.py ✅ ✅ Ⓜ *_test.py ✅ Ⓜ *_tests.py Ⓒ *(unittest.TestCase) ✅ ✅ ⓜ test_* ✅ ✅ Ⓒ Test* ✅ ⓜ test_* ✅ ⓕ test_* ✅
  44. @easyaspython !44

  45. @easyaspython !45

  46. @easyaspython Option A: Rename test modules !46

  47. @easyaspython Option B: pytest.ini !47

  48. @easyaspython !48

  49. @easyaspython !49

  50. @easyaspython !50

  51. @easyaspython Step 4: Shim things !51

  52. @easyaspython conftest.py !52

  53. @easyaspython !53

  54. @easyaspython !54

  55. @easyaspython Step 5: Use pytest in new tests !55

  56. @easyaspython Step 6: Rewrite old tests !56

  57. @easyaspython Step 7: Use helpful plugins !57

  58. @easyaspython Step 8: Integrate with CI/CD !58

  59. @easyaspython Incremental !59

  60. @easyaspython Helpful fallbacks !60

  61. @easyaspython Adopt and embrace !61

  62. @easyaspython What else is a pytest? !62

  63. @easyaspython Parameterization !63

  64. @easyaspython !64

  65. @easyaspython !65

  66. @easyaspython !66

  67. @easyaspython !67

  68. @easyaspython !68

  69. @easyaspython !69

  70. @easyaspython !70

  71. @easyaspython !71

  72. @easyaspython !72

  73. @easyaspython Works with Jenkins !73

  74. @easyaspython Works with tox !74

  75. @easyaspython Plugin-based architecture (pluggy) !75

  76. @easyaspython pytest pytest-cov pytest-django pytest-randomly !76

  77. @easyaspython !77

  78. @easyaspython Thanks !78