Apologies if this is because I've configured something wrong in RStudio (I'm pretty new to R generally), or some other newbie error, but I'm testing a package I'm writing, and I have test folders set up (I think correctly) like so:
package_directory/tests/testhat/[my tests]
package_directory/tests/testthat.R
However when I run test_check([my package]) it says:
Error: No tests found for [my package]
However if I move my tests here:
package_directory/testhat/[my tests]
Then test_check([my package]) works fine..
It seems like test_package() is looking for tests in:
package_directory/testhat/
when it should be looking for tests in
package_directory/tests/testhat/
Are all your tests named test-xyz.R
?
Thanks for replying quickly. Yes they all start with test.
Moving the folder location fixed the issue and if I move it back I get the same error so I'm pretty confident the folder location is the issue.
I checked your test_check function - it looks for test files in the testthat folder within the current directory. In my case the current directory is the working directory/package folder so it would work if it instead looked in tests/testthat of the working directory.
So it looks to me like an issue with the code in this function - but perhaps the current directory should be the tests folder because that's where the script is - in which case the issue is that my setup isn't behaving like that.
I'm pretty sure the folder location is not the problem because it works for me. What's your working directory?
If you can share the package with me (over email if needed) I can take a look.
My working directory is the project folder:
"C:/Users/s6mike/Documents/R/gauditr"
Thanks! I've just shared my "gauditr" private repo with you.
The key difference between the repo and my working directory is that I have a junction in the working folder called testthat which links to /tests/testthat. This allows test_check() to work - when I delete the junction it gets the error:
Error: No tests found for gauditr
It appears to work fine for me:
==> devtools::test()
Testing gauditr
Error in paste("Create", gl_api_package, "instance") :
object 'gl_api_package' not found
Calls: <Anonymous> ... <Anonymous> -> <Anonymous> -> .oapply -> eval -> paste
Execution halted
You shouldn't really have any code in tests/thatthat.R
Thanks for taking the trouble to check....
However, devtools::test() and all the other test functions work fine for me too - it's specifically the test_check() function which seems to look in the wrong folder. Sorry I should have explicitly mentioned that the other test commands were working ok.
And thanks for the advice re code in testthat.R
I know the testthat.R file isn't meant to contain extra code but at present my package needs some variables in the global environment to work (partly due to bad design in another package I was relying on).
I'm changing my package to avoid this requirement, but in the meantime by putting the global variables in testthat.R and then sourcing it I can create the variables I need and tests all my test files in one go. I know that wasn't the original purpose of the file but it simplifies my current workflow.
Presumably I could move the code elsewhere and use assign to create the variables I need in global but that also seemed awkward. If there's a simpler way to create global variables when running tests I would love to know.
How are you running test_check()
? You shouldn't be calling it directly; it's just there to be called by R CMD check. That's the reason you shouldn't have code in tests/testthat.R
- it only gets called in one path to the tests. Maybe you should use a helper file instead?
By the way, how to set global variable for all test file in tests/testthat directory??? I need it! For eaxmple, if every test file need to use object (sc<-sparkR.init()) to test many methods whether to get work, should I need init this object(sc<-sparkR.init()) in every test file again and again ?
FYI It is very tempting to call test_package directly as per manual instead of devtools::test. I mean from R/RStudio prompt.
from command line can I call $project_dir/> Rscript tests/testthat.R
?
So if I want to run the test feature of R Studio I have to call the project "test-[projectname]?"
Most helpful comment
It appears to work fine for me:
You shouldn't really have any code in
tests/thatthat.R