SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
Unit Testing in R
(R Workflow Series Part 5)
Houston R Users Group
Ed Goodwin
R Workflow Series
• Intro to RStudio - January
• Using knitr for literate programming and
documentation - February
• Intro to GitHub - March
• Production code in R - March
• Unit Testing in R - You are here
Tying it all together
• RStudio has lots of debugging capabilities
• Production code can get messy really quickly
• The bigger a code base grows the easier it is to
make dumb mistakes that ripple through the
code
• How do we help manage all this?
The Answer is
Unit Testing*
* Unit Testing is not a “silver bullet”. It will not turn you into a programming
genius. However, it will help you fake it really well until you become one.
What is Unit Testing?
• Unit Tests are low level tests of your code
functionality.
• Written by the developers themselves
• The first line of defense against bugs in the
code
Test Driven Development (TDD)
• Unit testing gained popularity with Test Driven
Development in the late 1990s. The idea was that you
write the test first and then write the code that it tested
• Test Driven Development philosophy helped make
eXtreme Programming (XP) popular
• eXtreme Programming was rolled into Agile Methodologies
that are now dominant today
• see http://martinfowler.com/bliki/
TestDrivenDevelopment.html “Test Driven Development:
By Example” by Martin Fowler for more information
Benefits of Unit Testing
• Fewer bugs
• Better code structure
• Easier restarts…gives you confidence to make
changes
• Robust code
Test Driven Development
Steps
1. Write a test for the next bit of functionality that
you want to test
2. Write the functional code until the test passes
3. Refactor the new and old code to make it well
structured
Tools for unit testing
• Some functionality you want to code
• A unit test model that will allow you to create
tests easily
• A unit test scaffolding to run the tests quickly in
the order you want
• A unit test reporter that will tell you the results
of the test
Workflow
testthat package
• Designed by Hadley Wickham
“I wrote testthat because I discovered I was spending too
much time recreating bugs that I had previously fixed.”
• Provides functions that catch errors, warnings and
messages
• Allows for simple testing via command line, RStudio, or
other dev tools
• Integrates into R package system, for building code
intended for external consumption via CRAN
testthat
• Hierarchical structure
• Expectations - describes what the result of a
computation should be
• Tests - group together expectations
• Contexts - group together tests by related
functionality
• set up your package to use testthat with
devtools::use_testthat()
Expectations
• equals()
• all.equal()
• is_identical_to()
• is_equivalent_to()
• is_a()
• matches()
• prints_text()
• shows_message()
• gives_warning()
• throws_error()
• is_true()
Expectations (contd.)
R Journal, January 2011 “testthat: Get Started with Testing” by Hadley Wickham
Tests
testthat.R used to run all your tests…
as long as their names start with test (e.g. testMath.r)
use devtools::test(“/path/to/tests/“) to run
Example Test
Simple test that will pass
Expectations
Contexts
• called using the
context(“string”) function
• allows you to group
multiple tests together to
help identify failure points
• simply an output
mechanism
Skipping tests
skip() function allows you to skip tests.
output shows an ’S’ instead of a ‘.’ for skipped output.
Set up, Tear down, and
cleanup
testthat does not handle tear down and clean up.
You must do this explicitly yourself.
Continuous Integration
Continuous integration is the process of setting up your project to do continuous
builds and test.
If you have a GitHub project you can set this up with Travis CI (https://travis-
ci.org/)…it’s free if your project is open source.
Simply run the command devtools::add_travis() to create a travis.yml file to your
project.
Best practices of unit testing
A great unit test will do the following:
• Fail only when a bug has been introduced
• Fail Fast, for instant feedback
• Easy to understand what scenario is being
tested and needs fixing
Best practices (contd.)
• tests should run under a minute
• serialize and hardcode your data outside of the test...create
mockups (Test Doubles...like Stunt Doubles in the movies)
• offload connections and integration points
• avoid system variables (especially for CRAN)
• treat test code as production code...it's a feedback mechanism for
you
• eliminate unreliable tests
• test at the appropriate level...for your project.
Unit Test Design Patterns
Arrange, Act, Assert Pattern
Given, When, Then
Resources
• RJournal intro to testthat https://journal.r-project.org/archive/2011-1/
RJournal_2011-1_Wickham.pdf
• testthat Github https://github.com/hadley/testthat
• Martin Fowler’s Unit Test bliki http://martinfowler.com/bliki/UnitTest.html
• http://kbroman.org/pkg_primer/pages/tests.html
• xUnit test design patterns http://xunitpatterns.com/Book%20Outline.html
• http://www.typemock.com/unit-test-patterns-for-net
Sample Code for Talk
https://github.com/egoodwintx/hrugtestexample

Weitere ähnliche Inhalte

Was ist angesagt?

Note - (EDK2) HII Compile
Note - (EDK2) HII CompileNote - (EDK2) HII Compile
Note - (EDK2) HII Compile
boyw165
 

Was ist angesagt? (20)

Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
How to Use EXAchk Effectively to Manage Exadata Environments
How to Use EXAchk Effectively to Manage Exadata EnvironmentsHow to Use EXAchk Effectively to Manage Exadata Environments
How to Use EXAchk Effectively to Manage Exadata Environments
 
Fail-Safe Cluster for FirebirdSQL and something more
Fail-Safe Cluster for FirebirdSQL and something moreFail-Safe Cluster for FirebirdSQL and something more
Fail-Safe Cluster for FirebirdSQL and something more
 
Analysis of Database Issues using AHF and Machine Learning v2 - AOUG2022
Analysis of Database Issues using AHF and Machine Learning v2 -  AOUG2022Analysis of Database Issues using AHF and Machine Learning v2 -  AOUG2022
Analysis of Database Issues using AHF and Machine Learning v2 - AOUG2022
 
Oracle DBA
Oracle DBAOracle DBA
Oracle DBA
 
What to Expect From Oracle database 19c
What to Expect From Oracle database 19cWhat to Expect From Oracle database 19c
What to Expect From Oracle database 19c
 
Exadata MAA Best Practices
Exadata MAA Best PracticesExadata MAA Best Practices
Exadata MAA Best Practices
 
Oracle Data Integrator R12.2.1.1 Agentセットアップガイド
Oracle Data Integrator R12.2.1.1 AgentセットアップガイドOracle Data Integrator R12.2.1.1 Agentセットアップガイド
Oracle Data Integrator R12.2.1.1 Agentセットアップガイド
 
Service Connector Hub ご紹介
Service Connector Hub ご紹介Service Connector Hub ご紹介
Service Connector Hub ご紹介
 
DOAG Oracle Unified Audit in Multitenant Environments
DOAG Oracle Unified Audit in Multitenant EnvironmentsDOAG Oracle Unified Audit in Multitenant Environments
DOAG Oracle Unified Audit in Multitenant Environments
 
HOT Understanding this important update optimization
HOT Understanding this important update optimizationHOT Understanding this important update optimization
HOT Understanding this important update optimization
 
Note - (EDK2) HII Compile
Note - (EDK2) HII CompileNote - (EDK2) HII Compile
Note - (EDK2) HII Compile
 
Oracle Key Vault Overview
Oracle Key Vault OverviewOracle Key Vault Overview
Oracle Key Vault Overview
 
Oracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret InternalsOracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret Internals
 
Data guard oracle
Data guard oracleData guard oracle
Data guard oracle
 
Improve PostgreSQL replication with Oracle GoldenGate
Improve PostgreSQL replication with Oracle GoldenGateImprove PostgreSQL replication with Oracle GoldenGate
Improve PostgreSQL replication with Oracle GoldenGate
 
OOW16 - Oracle Enterprise Manager 13c Cloud Control for Managing Oracle E-Bus...
OOW16 - Oracle Enterprise Manager 13c Cloud Control for Managing Oracle E-Bus...OOW16 - Oracle Enterprise Manager 13c Cloud Control for Managing Oracle E-Bus...
OOW16 - Oracle Enterprise Manager 13c Cloud Control for Managing Oracle E-Bus...
 
ORACLE 12C DATA GUARD: FAR SYNC, REAL-TIME CASCADE STANDBY AND OTHER GOODIES
ORACLE 12C DATA GUARD: FAR SYNC, REAL-TIME CASCADE STANDBY AND OTHER GOODIESORACLE 12C DATA GUARD: FAR SYNC, REAL-TIME CASCADE STANDBY AND OTHER GOODIES
ORACLE 12C DATA GUARD: FAR SYNC, REAL-TIME CASCADE STANDBY AND OTHER GOODIES
 
ODA Backup Restore Utility & ODA Rescue Live Disk
ODA Backup Restore Utility & ODA Rescue Live DiskODA Backup Restore Utility & ODA Rescue Live Disk
ODA Backup Restore Utility & ODA Rescue Live Disk
 
Row-level security and Dynamic Data Masking
Row-level security and Dynamic Data MaskingRow-level security and Dynamic Data Masking
Row-level security and Dynamic Data Masking
 

Andere mochten auch

Intro to Forecasting - Part 3 - HRUG
Intro to Forecasting - Part 3 - HRUGIntro to Forecasting - Part 3 - HRUG
Intro to Forecasting - Part 3 - HRUG
egoodwintx
 
edwin cv for QA-QC Engineer
edwin cv for QA-QC Engineeredwin cv for QA-QC Engineer
edwin cv for QA-QC Engineer
Edwin Aguinaldo
 
Field Research methodologies_ Dr. M Shah Alam Khan
Field Research methodologies_ Dr. M Shah Alam KhanField Research methodologies_ Dr. M Shah Alam Khan
Field Research methodologies_ Dr. M Shah Alam Khan
SaciWATERs
 

Andere mochten auch (20)

Collaborative Package Development in R
Collaborative Package Development in RCollaborative Package Development in R
Collaborative Package Development in R
 
Intro to Forecasting - Part 3 - HRUG
Intro to Forecasting - Part 3 - HRUGIntro to Forecasting - Part 3 - HRUG
Intro to Forecasting - Part 3 - HRUG
 
Building interactive web app with shiny
Building interactive web app with shinyBuilding interactive web app with shiny
Building interactive web app with shiny
 
Fantasy Football Draft Optimization in R - HRUG
Fantasy Football Draft Optimization in R - HRUGFantasy Football Draft Optimization in R - HRUG
Fantasy Football Draft Optimization in R - HRUG
 
SFSmith Final Paper
SFSmith  Final PaperSFSmith  Final Paper
SFSmith Final Paper
 
SAP SD Training in Chennai Demo Part-4
SAP SD Training in Chennai Demo Part-4SAP SD Training in Chennai Demo Part-4
SAP SD Training in Chennai Demo Part-4
 
PRINCE CV -
PRINCE CV - PRINCE CV -
PRINCE CV -
 
7 Ways To Know Your Strategy Planning Session Will Fail
7 Ways To Know Your Strategy Planning Session Will Fail7 Ways To Know Your Strategy Planning Session Will Fail
7 Ways To Know Your Strategy Planning Session Will Fail
 
Bn1029 demo sap sd
Bn1029 demo  sap sdBn1029 demo  sap sd
Bn1029 demo sap sd
 
Diplomarbeit florian weichelt
Diplomarbeit florian weicheltDiplomarbeit florian weichelt
Diplomarbeit florian weichelt
 
Internet Of Things
Internet Of ThingsInternet Of Things
Internet Of Things
 
WordPress for Girl Geeks 2009-11-24
WordPress for Girl Geeks 2009-11-24WordPress for Girl Geeks 2009-11-24
WordPress for Girl Geeks 2009-11-24
 
Mission Possible
Mission PossibleMission Possible
Mission Possible
 
St martin mit quellen
St martin mit quellenSt martin mit quellen
St martin mit quellen
 
edwin cv for QA-QC Engineer
edwin cv for QA-QC Engineeredwin cv for QA-QC Engineer
edwin cv for QA-QC Engineer
 
1 moodboard
1 moodboard1 moodboard
1 moodboard
 
Data mining with caret package
Data mining with caret packageData mining with caret package
Data mining with caret package
 
Field Research methodologies_ Dr. M Shah Alam Khan
Field Research methodologies_ Dr. M Shah Alam KhanField Research methodologies_ Dr. M Shah Alam Khan
Field Research methodologies_ Dr. M Shah Alam Khan
 
Mobile and IoT testing
Mobile and IoT testingMobile and IoT testing
Mobile and IoT testing
 
Ch10 Field Research
Ch10 Field ResearchCh10 Field Research
Ch10 Field Research
 

Ähnlich wie Unit Testing in R with Testthat - HRUG

Introduction to-automated-testing
Introduction to-automated-testingIntroduction to-automated-testing
Introduction to-automated-testing
BestBrains
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Meilan Ou
 

Ähnlich wie Unit Testing in R with Testthat - HRUG (20)

A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
 
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAPABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
 
Unit Testing in JavaScript
Unit Testing in JavaScriptUnit Testing in JavaScript
Unit Testing in JavaScript
 
Bootstrapping Quality
Bootstrapping QualityBootstrapping Quality
Bootstrapping Quality
 
TDD in the ABAP world - sitNL 2013 edition
TDD in the ABAP world - sitNL 2013 editionTDD in the ABAP world - sitNL 2013 edition
TDD in the ABAP world - sitNL 2013 edition
 
Topic production code
Topic production codeTopic production code
Topic production code
 
DevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroDevOps - Boldly Go for Distro
DevOps - Boldly Go for Distro
 
Continuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIContinuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CI
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated Testing
 
Introduction to-automated-testing
Introduction to-automated-testingIntroduction to-automated-testing
Introduction to-automated-testing
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Tdd
TddTdd
Tdd
 
Tools for Software Testing
Tools for Software TestingTools for Software Testing
Tools for Software Testing
 
2015 msu-code-review
2015 msu-code-review2015 msu-code-review
2015 msu-code-review
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Tdd
TddTdd
Tdd
 

Mehr von egoodwintx (9)

HRUG - Linear regression with R
HRUG - Linear regression with RHRUG - Linear regression with R
HRUG - Linear regression with R
 
Scaling in R
Scaling in RScaling in R
Scaling in R
 
Boardgamegeek scraping
Boardgamegeek scrapingBoardgamegeek scraping
Boardgamegeek scraping
 
HRUG - Text Mining to Construct Causal Models
HRUG - Text Mining to Construct Causal ModelsHRUG - Text Mining to Construct Causal Models
HRUG - Text Mining to Construct Causal Models
 
Intro to RStudio
Intro to RStudioIntro to RStudio
Intro to RStudio
 
Intro to Forecasting in R - Part 4
Intro to Forecasting in R - Part 4Intro to Forecasting in R - Part 4
Intro to Forecasting in R - Part 4
 
Intro To Forecasting - Part 2 - HRUG
Intro To Forecasting - Part 2 - HRUGIntro To Forecasting - Part 2 - HRUG
Intro To Forecasting - Part 2 - HRUG
 
Eag 201110-hrugregexpresentation-111006104128-phpapp02
Eag 201110-hrugregexpresentation-111006104128-phpapp02Eag 201110-hrugregexpresentation-111006104128-phpapp02
Eag 201110-hrugregexpresentation-111006104128-phpapp02
 
Hrug intro to forecasting
Hrug intro to forecastingHrug intro to forecasting
Hrug intro to forecasting
 

Kürzlich hochgeladen

Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
wsppdmt
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
Health
 
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Bertram Ludäscher
 
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Abortion pills in Riyadh +966572737505 get cytotec
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
vexqp
 

Kürzlich hochgeladen (20)

Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptx
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
 
Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdf
 
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
 
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 

Unit Testing in R with Testthat - HRUG

  • 1. Unit Testing in R (R Workflow Series Part 5) Houston R Users Group Ed Goodwin
  • 2. R Workflow Series • Intro to RStudio - January • Using knitr for literate programming and documentation - February • Intro to GitHub - March • Production code in R - March • Unit Testing in R - You are here
  • 3. Tying it all together • RStudio has lots of debugging capabilities • Production code can get messy really quickly • The bigger a code base grows the easier it is to make dumb mistakes that ripple through the code • How do we help manage all this?
  • 4. The Answer is Unit Testing* * Unit Testing is not a “silver bullet”. It will not turn you into a programming genius. However, it will help you fake it really well until you become one.
  • 5. What is Unit Testing? • Unit Tests are low level tests of your code functionality. • Written by the developers themselves • The first line of defense against bugs in the code
  • 6. Test Driven Development (TDD) • Unit testing gained popularity with Test Driven Development in the late 1990s. The idea was that you write the test first and then write the code that it tested • Test Driven Development philosophy helped make eXtreme Programming (XP) popular • eXtreme Programming was rolled into Agile Methodologies that are now dominant today • see http://martinfowler.com/bliki/ TestDrivenDevelopment.html “Test Driven Development: By Example” by Martin Fowler for more information
  • 7. Benefits of Unit Testing • Fewer bugs • Better code structure • Easier restarts…gives you confidence to make changes • Robust code
  • 8. Test Driven Development Steps 1. Write a test for the next bit of functionality that you want to test 2. Write the functional code until the test passes 3. Refactor the new and old code to make it well structured
  • 9. Tools for unit testing • Some functionality you want to code • A unit test model that will allow you to create tests easily • A unit test scaffolding to run the tests quickly in the order you want • A unit test reporter that will tell you the results of the test
  • 11. testthat package • Designed by Hadley Wickham “I wrote testthat because I discovered I was spending too much time recreating bugs that I had previously fixed.” • Provides functions that catch errors, warnings and messages • Allows for simple testing via command line, RStudio, or other dev tools • Integrates into R package system, for building code intended for external consumption via CRAN
  • 12. testthat • Hierarchical structure • Expectations - describes what the result of a computation should be • Tests - group together expectations • Contexts - group together tests by related functionality • set up your package to use testthat with devtools::use_testthat()
  • 13. Expectations • equals() • all.equal() • is_identical_to() • is_equivalent_to() • is_a() • matches() • prints_text() • shows_message() • gives_warning() • throws_error() • is_true()
  • 14. Expectations (contd.) R Journal, January 2011 “testthat: Get Started with Testing” by Hadley Wickham
  • 15. Tests testthat.R used to run all your tests… as long as their names start with test (e.g. testMath.r) use devtools::test(“/path/to/tests/“) to run
  • 16. Example Test Simple test that will pass Expectations
  • 17. Contexts • called using the context(“string”) function • allows you to group multiple tests together to help identify failure points • simply an output mechanism
  • 18. Skipping tests skip() function allows you to skip tests. output shows an ’S’ instead of a ‘.’ for skipped output.
  • 19. Set up, Tear down, and cleanup testthat does not handle tear down and clean up. You must do this explicitly yourself.
  • 20. Continuous Integration Continuous integration is the process of setting up your project to do continuous builds and test. If you have a GitHub project you can set this up with Travis CI (https://travis- ci.org/)…it’s free if your project is open source. Simply run the command devtools::add_travis() to create a travis.yml file to your project.
  • 21. Best practices of unit testing A great unit test will do the following: • Fail only when a bug has been introduced • Fail Fast, for instant feedback • Easy to understand what scenario is being tested and needs fixing
  • 22. Best practices (contd.) • tests should run under a minute • serialize and hardcode your data outside of the test...create mockups (Test Doubles...like Stunt Doubles in the movies) • offload connections and integration points • avoid system variables (especially for CRAN) • treat test code as production code...it's a feedback mechanism for you • eliminate unreliable tests • test at the appropriate level...for your project.
  • 23. Unit Test Design Patterns Arrange, Act, Assert Pattern Given, When, Then
  • 24. Resources • RJournal intro to testthat https://journal.r-project.org/archive/2011-1/ RJournal_2011-1_Wickham.pdf • testthat Github https://github.com/hadley/testthat • Martin Fowler’s Unit Test bliki http://martinfowler.com/bliki/UnitTest.html • http://kbroman.org/pkg_primer/pages/tests.html • xUnit test design patterns http://xunitpatterns.com/Book%20Outline.html • http://www.typemock.com/unit-test-patterns-for-net
  • 25. Sample Code for Talk https://github.com/egoodwintx/hrugtestexample