what software engineers can share with data scientists: with automatic...
TRANSCRIPT
![Page 1: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/1.jpg)
Andrea [email protected]
What Software Engineers can share with Data Scientists:
… with Automatic Tests
sponsored by
![Page 2: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/2.jpg)
Outline
1. Conway’s Game of Life2. Why do tests
2.1. Validation2.2. Working documentation2.3. Readable code
3. Testing Strategies3.1. Test Driven Development (TDD)3.2. Test After Development (TAD)
4. Testing tools4.1. Testthat package4.2. Shinytest package
Summary
![Page 3: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/3.jpg)
sponsored by
1. Conway’s Game of Life
Dead
Alive
Legend:
![Page 4: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/4.jpg)
sponsored by
1. Conway’s Game of Life
Dead
Alive
Legend:
http://jonathan-jackson.net/life-in-a-shade-of-rubyhttps://en.wikipedia.org/wiki/Conway's_Game_of_Life
![Page 5: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/5.jpg)
Outline
1. Conway’s Game of Life2. Why do tests
2.1. Validation2.2. Working documentation2.3. Readable code
3. Testing Strategies3.1. Test Driven Development (TDD)3.2. Test After Development (TAD)
4. Testing tools4.1. Testthat package4.2. Shinytest package
Summary
![Page 6: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/6.jpg)
sponsored by
2. Why do tests?
1. Validation2. Working documentation3. Readable and reusable code
![Page 7: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/7.jpg)
sponsored by
2.1. ValidationContext: Evolvecontext(“evolution of a single cell”)
…
2. Why do tests?
![Page 8: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/8.jpg)
sponsored by
2.1. ValidationContext: Evolve
1. Test 1: rule 11.1. ...
2. Test 2: rule 22.1. ...
3. Test 3: rule 33.1.
4. Tests ...
context(“evolution of a single cell”)…
test_that(desc = “rule1”,...)
test_that(desc = “rule2”,...)
test_that(desc = “rule3”,...)
test_that(desc = “rule4”,...)
2. Why do tests?
![Page 9: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/9.jpg)
sponsored by
2.1. ValidationContext: Evolve
1. Test 1: rule 11.1. ...
2. Test 2: rule 22.1. ...
3. Test 3: rule 33.1.
4. Tests ...
context(“evolution of a single cell”)… test_that(desc = paste("Any dead cell", "with exactly three live neighbours", "becomes a live cell,", "as if by reproduction."),
...
2. Why do tests?
![Page 10: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/10.jpg)
sponsored by
2.1. ValidationContext: Evolve
1. Test 1: rule 11.1. ...
2. Test 2: rule 22.1. ...
3. Test 3: rule 33.1. Setup3.2. Function run3.3. Validation
4. Tests ...
context(“evolution of a single cell”)… test_that(desc = paste("Any dead cell", "with exactly three live neighbours", "becomes a live cell,", "as if by reproduction."), code = { state <- dead
evolved_state <- evolve(state, neigb = 3)
expect_equal(evolved_state, alive) })...
2. Why do tests?
![Page 11: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/11.jpg)
sponsored by
2.2. Working documentation
A test file provides:
1. Information about the feature (context)2. Some working examples of how that feature is
implemented (test_that)
2. Why do tests?
![Page 12: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/12.jpg)
sponsored by
2.3. Readable codeRefactoring: improving the code without adding further functionalities.
if (wday(now) > 2 && wday(now) < 6 && hour(now) > 8 && hour(now) < 17 ) { cat("I'm working.")} else { cat("I'm out of the office.") }
2. Why do tests?
![Page 13: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/13.jpg)
sponsored by
2.3. Readable codeRefactoring: improving the code without adding further functionalities.
if (wday(now) > 2 && wday(now) < 6 && hour(now) > 8 && hour(now) < 17 ) { cat("I'm working.")} else { cat("I'm out of the office.") }
if (is_working_time(now)) { cat("I'm working.")} else { cat("I'm out of office.") }
is_working_time <- function(time) { wday(time) > 2 && wday(time) < 6 && hour(time) > 8 && hour(time) < 17 }
2. Why do tests?
![Page 14: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/14.jpg)
Outline
1. Conway’s Game of Life2. Why do tests
2.1. Validation2.2. Working documentation2.3. Readable code
3. Testing Strategies3.1. Test Driven Development (TDD)3.2. Test After Development (TAD)
4. Testing tools4.1. Testthat package4.2. Shinytest package
Summary
![Page 15: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/15.jpg)
sponsored by
3. Testing Strategies
1. Test Driven Development (TDD)
Tests then Implementation
2. Test After Development (TAD)
Implementation then Tests
![Page 16: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/16.jpg)
sponsored by
3.1. Test Driven Development (TDD)
Add a Test
TestsPass
Fail
3. Testing Strategies
![Page 17: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/17.jpg)
sponsored by
3.1. Test Driven Development (TDD)
Add a Test
Tests
Make slight changes
Tests
Pass
Fail
Fail
3. Testing Strategies
![Page 18: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/18.jpg)
sponsored by
3.1. Test Driven Development (TDD)
Add a Test
Tests
Make slight changes
Tests
Pass
Fail
Fail
Refactor
Make slight changes
Tests
Refactor
Tests
3. Testing Strategies
![Page 19: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/19.jpg)
sponsored by
3.2. Test After Development (TAD)
Test After Development (TAD)
Implementation then Tests
1. Old way of operate, I only need to add tests to my implementation
2. It is always available3. Useful when the result can’t be predicted (models, ...)
3. Testing Strategies
![Page 20: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/20.jpg)
Outline
1. Conway’s Game of Life2. Why do tests
2.1. Validation2.2. Working documentation2.3. Readable code
3. Testing Strategies3.1. Test Driven Development (TDD)3.2. Test After Development (TAD)
4. Testing tools4.1. Testthat package4.2. Shinytest package
Summary
![Page 21: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/21.jpg)
sponsored by
4. Testing tools
1. Testthat package (TDD + TAD)
2. Shinytest package (TAD)
![Page 22: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/22.jpg)
sponsored by
4.1. Testthat Package- Complete set Testing tools- Developed by RStudio- Compatible with different Testing Strategies (TAD and TDD)
4. Testing tools
![Page 23: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/23.jpg)
sponsored by
4.1. Testthat PackageContext: Evolve
1. Test 1: rule 11.1. ...
2. Test 2: rule 22.1. ...
3. Test 3: rule 33.1. Setup3.2. Function run3.3. Validation
4. Tests ...
context(“evolution of a single cell”)… test_that(desc = paste("Any dead cell", "with exactly three live neighbours", "becomes a live cell,", "as if by reproduction."), code = { state <- dead
evolved_state <- evolve(state, neigb = 3)
expect_equal(evolved_state, alive) })...
4. Testing tools
![Page 24: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/24.jpg)
sponsored by
The strategy is TAD (Test After Development), therefore:
1. Having a working Shiny application2. Record a test using the application as the final user3. Run all tests sequentially4. And if something is wrong….
4.2. Shinytest4. Testing tools
![Page 25: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/25.jpg)
sponsored by
4.2. Shinytest4. If something is wrong… Get notified graphically!
You see the differences between the recorded run and the current coloured out.
4. Testing tools
![Page 26: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/26.jpg)
sponsored by
Summary
Why are tests so important in our work?
![Page 27: What Software Engineers can share with Data Scientists: with Automatic Tests2018.erum.io/slides/regular talks/Andrea Melloncelli.pdf · 2019-01-11 · What Software Engineers can](https://reader030.vdocuments.site/reader030/viewer/2022040615/5f0d5a5d7e708231d439edda/html5/thumbnails/27.jpg)
sponsored by
Questions?