Giter Club home page Giter Club logo

easytest's Introduction

easytest

Overview

I will admit it - I'm lazy. easytest is for people like me.

I want to incorporate great unit tests throughout my analyses using testthat but often the additional effort to write out something like the code below prevents from being as thorough as I could/should be when I'm in the midst of an analysis.

library(testthat)

### SOME OPERATION ON my_data_frame

testthat::test_that("I didn't accidentally create any duplicates in my dataframe",
                    expect_equal(distinct(my_data_frame), my_data_frame))

The thing is - testthat is a very robust library that provides testing tools for all manner of situations including package development.

What I found is that during the course of a data analysis that I was typically doing the same kinds of tests again and again: checking for duplicates, verifying row counts, summing up columns etc; I didn't really need the full power and flexibility that testthat provides.

The goal of easytest is to provide make testing in these kinds of scenarios so painlessly easy that you almost forget not to do it:

library(easytest)

### SOME OPERATION ON my_data_frame

my_data_frame %>% test_no_duplicates()

Voilà! We have essentially done the exact same thing as in the chunk above but the named function and pipe-ability makes it practically seamless. And less friction means more testing means - hopefully - better code.

Installation

You can install easytest from Github using the following command:

# You must have remotes installed first
remotes::install_github("joshmuncke/easytest")

Usage

All easytest tests have the test_ naming convention and take a dataframe as the first argument for ease of pipe-ing using magrittr. They silently return the same dataframe so you can utilize them within pipe chains.

mtcars %>%
  test_expected_rows(32) %>%
  summarize(averagedisp = mean(disp))

Available tests

The available tests reflect the types of tests I find myself doing most frequently during the course of an analysis. If you feel that a particularly useful test function should be added - please feel free to raise an issue or submit a PR!

Dataframe tests

  • test_no_duplicates: Test that there are no duplicates in the dataframe. If column names are provided then only these subset of columns will be checked for duplicates.
  • test_complete: Test that there are no missing values in the dataframe. Column names can also be provided to limit the test scope.
  • test_n_rows: Test that the dataframe has an expected number of rows.
  • test_same_as_other_df: Test that the dataframe is the same as another.
  • test_equal_rows: Test that the dataframe has the same number of rows as another (even if the content is different).

Column tests

  • test_column_sum: Test that the sum of a specific column in the dataframe equals a specific number.
  • test_column_n_unique_values: Test that there are a specific number of unique values in the column.
  • test_column_max: Test that the maximum value of a column does not exceed a specified value.
  • test_column_min: Test that the minimum value of a column is not less than specified value.
  • test_column_between: Test that all values of a column are within an expected range.
  • test_column_mean: Test that the mean value of a column has a specified value (within a certain number of standard deviations).

Isn't this just like assertr?

Pretty much! This package is a lot like a stripped down version of the fantastic assertr package - which you should totally check out and try for yourself. I just happen to prefer the syntax used here.

easytest's People

Contributors

joshmuncke avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.