我想知道如何测试生成图形的函数.我有一个简单的绘图功能img:
img <- function() {
plot(1:10)
}
Run Code Online (Sandbox Code Playgroud)
在我的包中,我喜欢使用这个函数创建一个单元测试testthat.因为plot和它的朋友在基础图形只是返回NULL一个简单
expect_identical的不工作:
library("testthat")
## example for a successful test
expect_identical(plot(1:10), img()) ## equal (as expected)
## example for a test failure
expect_identical(plot(1:10, col="red"), img()) ## DOES NOT FAIL!
# (because both return NULL)
Run Code Online (Sandbox Code Playgroud)
首先,我考虑绘制到文件中并比较md5校验和以确保函数的输出相等:
md5plot <- function(expr) {
file <- tempfile(fileext=".pdf")
on.exit(unlink(file))
pdf(file)
expr
dev.off()
unname(tools::md5sum(file))
}
## example for a successful test
expect_identical(md5plot(img()),
md5plot(plot(1:10))) ## equal (as expected)
## example for …Run Code Online (Sandbox Code Playgroud) 我有很多生成绘图的函数,通常使用ggplot2.现在,我正在生成情节并测试基础数据.但我想知道是否有合理的方法来测试该图包含我期望的图层/选项或图形元素是否符合预期.
例如:
library(ggplot2)
library(scales) # for percent()
library(testthat)
df <- data.frame(
Response = LETTERS[1:5],
Proportion = c(0.1,0.2,0.1,0.2,0.4)
)
#' @export plot_fun
plot_fun <- function(df) {
p1 <- ggplot(df, aes(Response, Proportion)) +
geom_bar(stat='identity') +
scale_y_continuous(labels = percent)
return(p1)
}
test_that("Plot returns ggplot object",{
p <- plot_fun(df)
expect_is(p,"ggplot")
})
test_that("Plot uses correct data", {
p <- plot_fun(df)
expect_that(df, equals(p$data))
})
Run Code Online (Sandbox Code Playgroud)
这就是我被困住的地方
test_that("Plot layers match expectations",{
p <- plot_fun(df)
expect_that(...,...)
})
test_that("Scale is labelled percent",{
p <- plot_fun(df)
expect_that(...,...)
})
Run Code Online (Sandbox Code Playgroud)
也许有更直接的方法?
在这种方法中,最适合放置测试数据文件的地方是什么?我指的是仅由测试/测试中的测试脚本使用的文件,但不是R /中的任何其他函数.
我当前的方法是将它们放在tests/testdata中,然后使用相对路径而不是system.file从那里读取.(为了避免需要安装包来运行测试).
到目前为止,是否有任何最佳实践?
我正在为一个函数编写测试,在某些条件下会产生警告.我想确保在其他条件下它不会产生警告.我没有看到一个明显的方法来轻松测试testthat.我想我可以这样做:
my.result <- 25
my.func <- function() my.result
expect_equal(
withCallingHandlers(
my.func(), warning=function() stop("A Warning!")
),
my.result
)
Run Code Online (Sandbox Code Playgroud)
或使用options(warn=2),但我希望有类似的东西:
expect_no_warnings(my.func())
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗
有显然是为了整合两种方式testthat使用R CMD check.我无法上班.
根据devtools维基:
在开发包时,将测试放在inst/tests中,然后创建一个文件tests/run-all.R(注意它必须是大写R),其中包含以下代码:
library(testthat)
library(mypackage)
test_package("mypackage")
Run Code Online (Sandbox Code Playgroud)
这将评估您在包命名空间中的测试(因此您可以测试非导出的函数),如果有任何测试失败,它将抛出错误.这意味着您将看到测试失败的完整报告,并且除非所有测试都通过,否则R CMD检查将不会通过.
整个包裹都在这里.它是两个文件:
## minimalbugexample/inst/tests/run-all.R
library(testthat)
library(minimalbugexample)
test_package('minimalbugexample')
Run Code Online (Sandbox Code Playgroud)
和
## minimalbugexample/inst/tests/test-use-Matrix-package.R
context("Intentional break")
expect_that( TRUE, equals(FALSE))
Run Code Online (Sandbox Code Playgroud)
我的描述是
Package: minimalbugexample
Title:
Description:
Version: 0.1.1
Author: Nathan VanHoudnos <nathanvan@letterafterFmail.com>
Maintainer: Nathan VanHoudnos <nathanvan@letterafterFmail.com>
Depends:
R (>= 3.0.1),
Matrix (>= 1.0)
Suggests:
testthat
License: GPL
LazyData: true
Collate:
'minimalbugexample-package.r'
'use-Matrix-package.R'
Run Code Online (Sandbox Code Playgroud)
安装软件包后,我可以正常运行测试(它们会失败,如预期的那样).
> test_package('minimalbugexample')
Intentional break : 1
1. Failure: -------------------------------------------------------------------
TRUE not equal to FALSE …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以在R中使用testthat测试框架来设置相等的容差.
目前,如果example.R是:
library(testthat)
three_times<-function(x) 3*x
context('Test three_times')
test_that('Three times returns 3 times x',{
expect_equal(three_times(3),9)
expect_equal(three_times(pi),9.4247)
})
Run Code Online (Sandbox Code Playgroud)
并执行test_file('example.R','stop'),第一次测试通过,但第二次测试失败:
Error: Test failed: 'Three times returns 3 times x'
Not expected: three_times(pi) not equal to 9.4247
Mean relative difference: 8.271963e-06.
Run Code Online (Sandbox Code Playgroud)
是否可以为平均相对差异设置更高的误差阈值?例如1e-3.我有一些只有3位小数精度的预期结果,这意味着现在我的测试总是失败...
什么是正确使用expect_error()的testthat包?我试图从帮助中调整示例,但是当我在错误消息中使用括号时,这会失败.
library(testthat)
# Works
tmp1 <- function() stop("Input is not correct")
expect_error(tmp1(),"Input is not correct")
# Does not work
tmp2 <- function() stop("Input (x) is not correct")
expect_error(tmp2(),"Input (x) is not correct")
# Does not work
tmp3 <- function() stop("Input(")
expect_error(tmp3(),"Input(")
Run Code Online (Sandbox Code Playgroud)
这导致:
> library(testthat)
>
> # Works
> tmp1 <- function() stop("Input is not correct")
> expect_error(tmp1(),"Input is not correct")
> # Does not work
> tmp2 <- function() stop("Input (x) is not correct")
> …Run Code Online (Sandbox Code Playgroud) 假设我有这样的测试:
require(testthat)
context("toy test")
test_that("toy", {
df = my.read.file("test.txt", header=TRUE)
expect_true(myfunc(df) == 3.14)
})
Run Code Online (Sandbox Code Playgroud)
并且这个测试依赖于外部文件test.txt,那么我应该把这个文件放在哪里呢?
我正在使用testthatR中的包,我正在尝试测试文件中定义的函数example.R.此文件包含一个调用source("../utilities/utilities.R"),其中包含utilities.R由我编写的函数的文件.但是,当我尝试测试函数时example.R,在测试脚本中获取它会产生以下错误:
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file '../utilities/utilities.R': No such file or directory
Run Code Online (Sandbox Code Playgroud)
您能否澄清一下如何在源自另一个文件的文件中运行函数测试?
我正在开发一个R应用程序并想出一个解决方法,通过向项目根目录添加一个文件来集成testthat(它通常需要你的项目成为一个包)DESCRIPTION.
我从同事那里获得了这种方法,并设法让它像这样工作.
问题是,当我尝试测试任何东西(甚至是空的测试文件)时,我收到此错误:
Error in x[[method]](...) : attempt to apply non-function
Calls: <Anonymous> ... <Anonymous> -> o_apply -> lapply -> FUN -> <Anonymous>
Execution halted
Run Code Online (Sandbox Code Playgroud)
我知道的是,这似乎只有在MacOS.我的同事仍可在Windows上运行此应用程序而不会出现问题.
这有什么用处?
用于重现此错误的MCVE将是:
创建DESCRIPTION包含内容的最小文件:
Package: testpckg
Run Code Online (Sandbox Code Playgroud)将testthat添加到您的项目中:
usethis::use_testthat()
usethis::use_test("foo")
Run Code Online (Sandbox Code Playgroud)bar.Rtest-foo.R,源栏:source("bar.R")RStudio版本1.1.447,R版本3.4.4,Mac OS X 10_13_4