问题
Actual question
Seems like devtools::test()
does not make sure that package dependencies as stated in a package's DESCRIPTION
file are loaded prior to running the unit tests. How can I change that?
Details
I'm writing a package (B
) that imports another one of my packages (A
).
When I try to run my unit tests via devtools::test()
, or, to be more precise via the shortcut SHFT + CRTL + T
in RStudio, certain tests fail as the imported package seems to be disregarded/not loaded and thus a certain function (isPackageInstalled
) can't be found.
Trying to load the imported package A
manually before running devtools::test()
didn't help either. I guess that's due to the fact that devtools
(or testthat
) "simulates" a fresh workspace state? Running the unit tests "one by one" works just fine after manually loading package A
beforehand, though.
I thought that devtools would look up package dependencies in the DESCRIPTION
file of B
and thus load them as would be the case when running require("B")
, but apparently not.
Here's my DESCRIPTION
file:
Package: B
Type: Package
Title: What the package does (short line)
Version: 0.1.0.1
Date: 2014-08-05
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
Imports: A
Here's the code I ran:
devtools::load_all() # or SHFT + CTRL + L in RStudio
devtools::test() # or SHFT + CTRL + T in RStudio
That's what RStudio's build
pane gave me:
==> devtools::test()
Loading required package: testthat
Testing B
Loading B
Creating a new generic function for 'signalCondition' in package 'B'
package : 1
package : ......
1. Error: getPackageDescription ------------------------------------------------
could not find function "isPackageInstalled"
1: expect_is(res <- getPackageDescription(), expected) at test-getPackageDescription.r:13
2: expect_that(object, is_a(class), info, label)
3: condition(object)
4: paste0(class(x), collapse = ", ")
5: getPackageDescription()
6: getPackageDescription() at Q:\home\wsp\rapp2\B/R/getPackageDescription.r:37
7: getPackageDescription(from = from, fields = fields, drop = drop, encoding = encoding,
...) at Q:\home\wsp\rapp2\B/R/getPackageDescription.r:154
8: getPackageDescription(from = from, fields = fields, drop = drop, encoding = encoding,
...) at Q:\home\wsp\rapp2\B/R/getPackageDescription.r:37
Am I missing something here?
Screenshot of build tools dialogue:
回答1:
The usual approach would be to use roxygen2
to automatically generate your NAMESPACE
file from special comments in your source code, but maintain your DESCRIPTION
file manually. There's no special stuff that I'm aware of to keep them in sync, but R CMD CHECK
will tell you if there's something missing/extra in your DESCRIPTION
.
来源:https://stackoverflow.com/questions/25385475/how-to-make-devtoolstest-consider-package-dependencies