README.md

testr

A Simple unit testing utility for R.

Usage

Import testr and the file to be tested into the spec file. Then use the testr methods in the structure shown below to test the imported function.

Methods

This package exposes the following global methods:

Example

# SomeFunctions.R
simpleAdd <- function(a, b) { return(a + b) }
simpleSubtract <- function(a, b) { return(a - b) }
simpleDivide <- function(a, b) {
    if (b == 0) {
        throw('Cannot divide by zero')
    } else {
        return(a/b)
    }
}

# SomeFunctions.spec.R
source(file = "testr.R", local = TRUE)
source(file = "SomeFunctions.R", local = TRUE)

describe('simpleAdd', function() {
    it('adds two values', function() {
        result <- simpleAdd(1, 1)

        expect(result)$toEqual(2)

        expect(simpleAdd(1, 2))$toEqual(3)
        expect(simpleAdd(1, 3))$toEqual(4)
    })

    it('adds two other values', function() {
        result <- simpleAdd(1, 1)

        expect(result)$toEqual(2)
    })

    it('failing test example - subtracts two values', function() {
        result <- simpleAdd(1, 1)

        expect(result)$toEqual(0) # This will cause the test to fail.
        expect(simpleAdd(1, 2))$toEqual(3) # Even though this condition passes, the test will fail b/c the first condition failed.
    })
})

describe('simpleSubtract', function() {
    it('adds two values', function() {
        result <- simpleSubtract(1, 1)

        expect(result)$toEqual(0)
    })

    it('failing test example - adds two values', function() {
        result <- simpleSubtract(1, 1)

        expect(result)$toEqual(2)
    })
})

describe('simpleDivide', function() {
    it('divides two values', function() {
        result <- simpleDivide(1, 1)

        expect(result)$toEqual(1)
    })

    it('throws an error when dividing by zero', function() {
        result <- function() { simpleDivide(1, 0) }

        expect(result)$toThrowAnError()
    })
})

And the output for the above example would be:

[1] "FAILED - simpleAdd"
[1] "     PASSED - adds two values"
[1] "     PASSED - adds two other values"
[1] "     FAILED - failing test example - subtracts two values"
[1] "          expected 2 to equal 0"

[1] "FAILED - simpleSubtract"
[1] "     PASSED - adds two values"
[1] "     FAILED - failing test example - adds two values"
[1] "          expected 0 to equal 2"

[1] "PASSED - simpleDivide"
[1] "     PASSED - divides two values"
[1] "     PASSED - throws an error when dividing by zero"


tmartin1/testr documentation built on Nov. 5, 2019, 10:54 a.m.