Uryu Shinya
August 22, 2018
740

# 転ばぬ先のテスト・アサートの導入〜再現性の保証対策として〜 / rcode-test-assert

⚡️ Lightning Talk ⚡️
Reproducible Source Code is here: https://github.com/uribo/talk_180822_rcode-test-assert

August 22, 2018

## Transcript

3. ### ৭ʑͳ৚݅ɾ؀ڥͷҧ͍͕͋Δ  04 3ຊମɾύοέʔδͷόʔδϣϯ มԽʢҟৗʣʹؾ͕ͭ͘͜ͱ͕େࣄ  ࢥ͍ࠐΈͷ··ղੳΛଓ͚ͯ͠·ͬͨΒʁ  Ͳ͜ͰมԽ͕͔͋ͬͨ୳͢ͷ͸ె࿑ 

Կ͕มΘͬͯ͠·ͬͨͷ͔Λ೺Ѳ͍ͨ͠ స σʔλ ͸ มΘΔ ςετɺΞαʔτΛಋೖ͢Δ͜ͱͰɺ σʔλͷมԽʹؾ෇͖΍͘͢ͳΔ
4. ### ୯ମςετ VOJUUFTU ͱݺ͹ΕΔ΋ͷ  ຊདྷ͸ɺϓϩάϥϜ͕ظ଴͞Εͨ௨Γʹɹɹ ಈ࡞͢Δ͔Λ֬ೝ͢ΔͨΊͷ࡞ۀ  هड़͞ΕͨςετύλʔϯΛࢼ͢ɹɹɹɹɹ ʢهड़ͷͳ͍ςετ݁Ռ͸อূ͠ͳ͍ʣ Ξαʔτ

BTTFSUʜஅݴ͢Δ   ஋ͷঢ়ଶΛ༧ଌ͢Δ஋Λهड़  ༧૝֎ͷ஋͕༩͑ΒΕͨࡍʹΤϥʔΛग़ྗ స ςετ Ξαʔτ ͱ

6. ### ✔ expect_equal( dim(iris), c(120, 4)) Error: dim(iris) not equal to

c(120, 4). 2/2 mismatches (average diff: 15.5) [1] 150 - 120 == 30 [2] 5 - 4 == 1 స FYQFDU@  ΦϒδΣΫτ ͱ ฦΓ஋ͷ ঢ়ଶΛهड़ expect_equal( object = dim(iris), expected = c(150, 5))
7. ### ؔ਺ ର৅ ྫ expect_equal() ฏ౳ੑ expect_equal(letters[1:3], c(“a", "b", “c”)) expect_setequal(letters[1:3],c("a",

"c", "b")) expect_gt() େখؔ܎ expect_gt(pi, 3.1) expect_gte(pi, 3.14) expect_lt(pi, 3.2) expect_true() ਅِ஋ expect_true(iris\$Species[1] == "setosa") expect_length() ௕͞ expect_length(unique(iris\$Species), 3)

9. ### EBUB JSJT ͷঢ়ଶΛه࿥ͨ͠ ɹɹɹɹɹɹɹɹɹͭͷςετ߲໨Λ༻ҙ tests/testthat/test-01iris.R  σʔλͷαΠζ ߦɾྻͷ਺  

ྻͷ໊લ  4QFDJFTྻʹؚ·ΕΔਫ४
10. ### tests/testthat/test-01iris.R ςετର৅ͷΦϒδΣΫτΛ ɹɹɹɹɹNZ@JSJTDTWʹม͑ͯ࠶౓࣮ߦ  NZ@JSJT͸JSJTͱಉ͡σʔλͰ͋Δ૝ఆ  JSJTʹର͍͔ͯͭ͘͠ͷՃ޻Λߦͬͨ΋ͷ Error: Test failed:

'Iris data statement' * dim(my_iris) not equal to c(150, 5). 1/2 mismatches [1] 120 - 150 == -30 * unique(my_iris\$Species) has length 4, not length 3. * levels(my_iris\$Species) not set-equal to c("setosa", "versicolor", "virginica"). Lengths differ: 4 is not 3 స ʮਖ਼͘͠ʯࣦഊ͢Δ

12. ### library(dplyr) iris %>% tibble::as_tibble() %>% select(Sepal.Length, Species) %>% verify( has_all_names(

c("Sepal.Length", "Species"))) %>% ﬁlter(Sepal.Length >= 5.0) %>% assert( within_bounds(5.0, Inf), Sepal.Length) ύΠϓॲཧ   ϑϨϯυϦʔ ͳ Ξαʔτ ؔ਺Λఏڙ verify has_all_names assert within_bounds ྻʹؚ·ΕΔ໊લͰ͋Δ ൣғʹؚ·ΕΔ஋Ͱ͋Δ

14. ### my_iris <- iris my_iris %>% select(Sepal.Length, Species) %>% ﬁlter(Sepal.Length >=

5.0) %>% group_by(Species) %>% summarise( sl_mean = mean(Sepal.Length)) # A tibble: 3 x 2 Species sl_mean <fct> <dbl> 1 setosa 5.23 2 versicolor 5.96 3 virginica 6.62 src/02-assertr_demo.R Ξαʔτ͕ͳ͍ͱʜ
15. ### my_iris <- read.csv( here::here("data", "my_iris.csv")) my_iris %>% select(Sepal.Length, Species) %>%

ﬁlter(Sepal.Length >= 5.0) %>% group_by(Species) %>% summarise( sl_mean = mean(Sepal.Length)) # A tibble: 4 x 2 Species sl_mean <fct> <dbl> 1 setosa 5.28 2 versicolor 5.98 3 virginica 6.65 4 Virsicolor 6.2 స ͜Ε͸๬Μͩ݁Ռ Ξαʔτ͕ͳ͍ͱʜ src/02-assertr_demo.R

17. ### ࡞ਤͨ͠ϑΝΠϧ΋ςετ͍ͨ͠  ϑΥϯτɺ৭ɺϨΠΞ΢τFUD  มߋΛՃ͑ͭͭɺաڈͷਤͱ΋ൺֱ͍ͨ͠ స ͦͷ ਤ͸ ຊ౰ʹ ಉ͡ʁ

ϙΠϯτͷେ͖͞ͱʜ  ୳͞ͳ͍Ͱ͍ͩ͘͞
18. ### ˞ύοέʔδ ؀ڥͰͷ ར༻Λ૝ఆ ࡞ਤΛߦ͏ॲཧ·ͨ͸ؔ਺Λ༻ҙ UFTUTUFTUUIBUͷதͷϑΝΠϧͰ ɹΛ࣮ߦ ·ͨ͸vdiffr::collect_cases(), vdiffr:::diffrAddin() Ͱ΋Մ vdiffr::validate_cases()Λ࣮ߦ

ˠUFTUTpHTʹ47(Ͱग़ྗ͞ΕΔ  ̎ ̏ 

21. ###  ςετΛॻ͘͜ͱͰੜ࢈ੑ͸ɹɹɹ Ұ࣍తʹԼ͕Δ  શͯΛ໢ཏ͢Δͷ͸ࠔ೉  σʔλ਺΍ܽଛɺྻͷσʔλܕͳͲɹ ͕େࣄʁ  ਖ਼͍͠ςετ͚ͩͰͳࣦ͘ഊ͢Δɹ

έʔεͷهड़΋ॏཁ స ԿΛ Ͳ͜·Ͱ ςετ ͢Δ͔ ɾ d࠷ޙʹd