[R] RUnit - need advice on a good directory structure or tips...

From: Sung Soo Kim <sskim.box_at_gmail.com>
Date: Sat 04 Feb 2006 - 17:46:59 EST


I made my own RUnit testing convention, and I want to introduce this to one of my friends.
Before that, I'd like to review my codes.

The problem that I met when I tried to polish my codes is: How can I get the file path in the file???
I.e., I want to get the path to the file that I'm writing using some magic R functions. But I couldn't find any good magic. Is there any magic?

So, I made a convention.

1. Put both "test....r" files and "suite...r" files in the same directory.
2. Current working path should be the same directory as that of test files.
3. In test files, use .setUp and .tearDown functions to properly modify
current working directory.
4. 'source' the source codes to test them.

This is pretty much restriction, and I want to reduce the number of restrictions.
More specifically, if the function "defineTestSuite" support optionally setting current working directory to that of test files, it would be great help, because it can remove the need of placing both test files and suite files in the same directory.
But the best solution is the way to get the path to the file of the current file.

BTW, here is an example. I welcome any kind of advice.


There are three files. Two are in the "tests" directory(suite_foo.r test_foo.r), and the other one is in the "codes" directory (foo.r).

In your R prompt, do "setwd("path to test directory")" and then do "source("suite_foo.r")" to run tests.


##################################################
## ../tests/test_foo.r
##
## A test file sample.
##
## Restrictions.
## Because of current directory problem, here I suggest convention.
##
## 1. The testSuite files and test files should be in the same directory
like this example.
##
## 2. Always include the following five objects in your own test file
## as it is shown in this example and modify them as you want.
## (-> pathToCode, codeFileName, currentPath, .setUp, .tearDown)
##
## 3. Then, properly change the pathToCode and the codeFileName.
## You can add your own codes to .setUP and .tearDown.
##
## 4. Use one test file for each code file. This is a good practice.
##
##################################################

pathToCode <- "../codes" ## relative path to code directory. codeFileName <- "foo.r" ## code file name in 'pathToCode' directory

currentPath <- getwd()
.setUp <- function() { ## called before each test case. Add your own codes.   setwd(paste(currentPath,pathToCode,sep="/"))   source(codeFileName)
  print(".setUp")
}
.tearDown <- function() { ## called before each test case. Add your own codes.
  setwd(currentPath)
  print(".tearDown")
}

##################################################
##
## Start of test functions.
##
## Add your own test functions with names starting with "test"
##
##################################################

test.c2f <- function() {
# print(currentPath)
  checkEquals(c2f(0), 32)
  checkEquals(c2f(10), 50)
  checkException(c2f("xx"))
}

test.f2c <- function() {
  checkEquals(f2c(32), 0)
  checkEquals(f2c(50), 10)
  checkTrue(identical(f2c(50),10))
  checkException(f2c("xx"))
}

test.errordemo <- function() {
  DEACTIVATED("Fanta long star king")
  stop("this is just to show what an error looks like as opposed to a failure")
}


##################################################
## ../tests/suite_foo.r
##
## A testSuite file sample
##
## Restrictions.
## Because of current directory problem, here I suggest convention.
##
## 1. A testSuite file should be in the same directory as that of test
files.
##
## 2. A testSuite file should be sourced (or executed)
## ONLY WHEN the 'current working directory' of R is in that of the
restSuite file.
##
## 3. The name of a testSuite file must start with 'suite', not 'test'.
##
##################################################

library("RUnit")

suite_foo <- defineTestSuite("foo",

            dirs = ".",
            testFileRegexp = "^test.+\\.r",
            testFuncRegexp = "^test.+"
            )

result_foo <- runTestSuite(suite_foo)
printTextProtocol(result_foo)
## printHTMLProtocol(result_foo, fileName="result_foo.html")
## runTestFile("test_foo.r")

##################################################
## ../codes/foo.r
##
## A code file sample
##
##################################################

## centigrade to fahrenheit

c2f <- function(a) return(9/5 * a + 32)

## fahrenheit to centigrade

f2c <- function(f) return(5/9 * (f - 32))


        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Feb 04 17:59:44 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:22 EST