私はちょうど今、R を使った単体テストを始めたばかりで、これまでのところ難しいそりを見つけています。私がやりたいことは、R コンソールに移動し、test()
testthat を入力して、R パッケージ内のすべてのファイルのテストを実行することです。
ここに私の環境があります:
sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin15.2.0 (64-bit)
Running under: OS X 10.11.4 (El Capitan)
そしてディレクトリ構造:
math
-- R
------ math.R
------ add.R
------ subtract.R
-- tests
------ testthat.R
------ testthat
---------- test_add.R
---------- test_subtract.R
---------- test_math.R
関連ファイルの次のサンプルを使用します。
数学.R
source('add.R')
source('subtract.R')
doubleAdd <- function(x){
return(add(x,x) + add(x,x))
}
add.R
add <- function(a,b){
return(a + b)
}
testthat.R
library(testthat)
library(math)
test_check("math")
test_add.R
context('add tests')
test_that('1 + 1 = 2', {
expect_equal(2, add(1,1))
})
エラー:
R コンソールでは、次の結果が得られます。
library(devtools)
test()
<b>Loading math
Loading required package: testthat
Error in file(filename, "r", encoding = encoding) (from math.R#1) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'add.R': No such file or directory
</b>
ただし、作業ディレクトリを切り替えてsetwd('R')
math.R を実行すると、正常にdoubleAdd
機能します。また、math.R を削除するか、「R」ディレクトリから math.R を移動すると、問題なくtest()
動作します。
test()
すべての R ファイルに対してテストを実行するには、これらのファイルをどのようにセットアップすればよいですか?