标签: testthat

测试连接的问题

任何人都有任何想法为什么textConnectiontest_that函数内部使用不能正常工作?

即如果我直接运行以下代码,一切都很好:

txt <- ""
con <- textConnection("txt", "w")  
writeLines("test data", con)
close(con)

expect_equal(txt, "test data")  #Works
Run Code Online (Sandbox Code Playgroud)

然而,如果我将其嵌入到test_that函数内部,它就不起作用,我得到一个空的txt变量来expect_equal调用.

test_that("connection will work", {

  txt <- ""
  con <- textConnection("txt", "w")  
  writeLines("test data", con)
  close(con)

  expect_equal(txt, "test data")  
}) #Fails
Run Code Online (Sandbox Code Playgroud)

会话信息

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
  [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

other …
Run Code Online (Sandbox Code Playgroud)

r testthat

5
推荐指数
1
解决办法
106
查看次数

即使test_package通过,auto_test_package也会抛出错误

我正在尝试使用R编写测试testthat.二者testthatdevtools使用已安装的install_github,测试代码是在tests/testthattest-all.R处于tests.使用R CMD检查我得到

  • 检查测试...运行'test-all.R'确定

> test()
测试FASTIN
加载FASTIN
IO测试:......

同样

> test_package('FASTIN')
IO测试:......

非常满意我的第一次测试经验,我现在想用`auto_test_package'自动监控进一步的开发,但是从包根目录调用9it给出:

> auto_test_package('./')
IO test:......
摘要中的错误(path,file = TRUE):指定的路径名​​不是文件:/ Users/phil/Work/FASTIN-R/FASTIN-R /测试/ testthat

我想这意味着测试运行但一旦完成就会出错?尝试手动设置它auto_test会产生相同的错误:

> auto_test('./ R /','./
test /')IO test:......
摘要错误(path,file = TRUE):指定的路径名​​不是文件:/ Users/phil /工作/ FASTIN-R/FASTIN-R /测试/ testthat

我尝试设置auto_test使用test/testthat,但这次测试运行但也抛出错误...

auto_test('./ R /','./ tests/testthat /')
IO测试:12
1.错误:SI导入正常工作-------------------- -------------------------------------------------- --------------------
nchar(SI.predators)> 0&nchar(SI.preys)> 0不为TRUE
1:addSI(SI.predators = SI.捕食者,SI.preys = SI.preys,Frac.Coeffs.mean = Frac.Coeffs.mean,Frac.Coeffs.var …

r devtools testthat

5
推荐指数
0
解决办法
130
查看次数

你如何测试自定义的期望?

假设我想要一个自定义的testthat期望.例如,我正在测试大量对象以查看它们是否没有缺失值.testhat写东西的方式应该是这样的:

expect_no_nas <- function(object, info = NULL, label = NULL)
{
  lab <- testthat:::make_label(object, label)
  expect(has_no_nas(object), sprintf("%s has nulls.", lab), 
    info = info)
  invisible(object)
}

has_no_nas <- function()
{
  !any(is.na(x))
}
Run Code Online (Sandbox Code Playgroud)

我如何测试那是对的?

我可以编写通过的测试,没问题.

test_that(
  "expect_no_nas passes when there are no NAs",
  {
    expect_no_nas(1:5)
  }
)
Run Code Online (Sandbox Code Playgroud)

我以为我可以包装自定义期望expect_error,但这不起作用:

test_that(
  "expect_no_nas fails when there are NAs",
  {
    expect_error(expect_no_nas(c(1, NA)))
  }
)   
## Error: Test failed: 'expect_no_nas fails when there are NAs'
## * Not expected: c(1, NA) …
Run Code Online (Sandbox Code Playgroud)

r testthat

5
推荐指数
1
解决办法
283
查看次数

如何在R testthat中同时测试消息和错误消息?

我希望对产生连接的函数进行单元测试。它在执行期间输出一条包含连接详细信息的消息。

我想测试以下内容:

  1. 该消息按预期显示 ( expect_message(fn(),"blah"))
  2. 没有错误 ( expect_error(fn(),NA))
  3. 创建的对象是一个特定的类 ( expect_is(fn(),"PostgreSQLConnection"))

我可以做res<-fn()然后expect_is()从它做,但是如何在调用函数时对消息和(缺少)错误执行测试。

理想情况下,我想同时评估所有三个,然后我可以安全地关闭连接。

library(testthat)
fn<-function(){
  message("blah")
  obj<-"blah"
  class(obj)<-c("PostgreSQLConnection",class(obj))
  return(obj)
}

expect_message(fn(),"blah")
expect_error(fn(),NA)
expect_is(fn(),"PostgreSQLConnection")
Run Code Online (Sandbox Code Playgroud)

PSexpect_messageexpect_error函数使用的函数throws_error可能会或可能不会被弃用 - 文档在这一点上有点混乱。?throws_error

r testthat

5
推荐指数
1
解决办法
559
查看次数

如何使用testthat测试已安装的软件包?

我想测试一个已安装的软件包,但这会返回一个错误.

library(testthat)
test_package("testthat")
# Error: No tests found for testthat
Run Code Online (Sandbox Code Playgroud)

test_package(source here)返回此错误,因为它system.file("tests", package = package)是空的.实际上,tests安装的软件包中缺少该目录.

list.dirs(system.file("", package = "testthat"))
# [1] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat/"     
# [2] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//help"
# [3] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//html"
# [4] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//libs"
# [5] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//Meta"
# [6] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//R"  
Run Code Online (Sandbox Code Playgroud)

如何安装包以使其测试目录仍然存在?

r testthat

5
推荐指数
2
解决办法
1200
查看次数

R tryCatch与testthat期望

我有以下功能:

fun = function(expr) {
  mc = match.call()

  env = as.environment(within(
    list(),
    expr = eval(mc$expr)
  ))

  return(env)
}
Run Code Online (Sandbox Code Playgroud)

在一个内部调用,tryCatch()以便expr优雅地处理任何错误条件.

它在标准错误条件下工作正常:

tryCatch({
  fun({
    stop('error')
  })
}, error = function(e) {
  message('error happened')
})

# error happened
Run Code Online (Sandbox Code Playgroud)

但是,它不会捕获testthat期望错误(这是我特定用例的首选):

library(testthat)

tryCatch({
  fun({
    expect_true(FALSE)
  })
}, error = function(e) {
  message('expectation not met')
})

# Error: FALSE isn't true.
Run Code Online (Sandbox Code Playgroud)

或更简单地说:

library(testthat)

tryCatch({
  expect_true(FALSE)
}, error = function(e) {
  message('expectation not met')
})

# Error: FALSE isn't true.
Run Code Online (Sandbox Code Playgroud)

期望错误未被捕获. …

r testthat

5
推荐指数
1
解决办法
355
查看次数

阅读登录Travis CI的详细测试

我工作的一个R项目与特拉维斯CI测试.当我在testthat本地运行测试时,所有测试都通过.当这些测试在Travis CI上运行时,一些测试失败了.

问题是我无法看到测试运行后出现的详细错误消息.

我确实看到了特拉维斯错误的"顶部":

ERROR

Running the tests in ‘tests/testthat.R’ failed.
Last 13 lines of output:
  OK: 825 SKIPPED: 0 FAILED: 9
  1. Error: one posterior is added (@test-add_posteriors.R#44) 
  2. Error: two posteriors are added (@test-add_posteriors.R#116) 
  3. Error: three posteriors are added, middle is deleted and added again (@test-add_posteriors.R#197) 
  4. Error: alignment_to_beast_posterior: basic (@test-alignment_to_beast_posterior.R#28) 
  5. Error: are_identical_posteriors: use from local simulation (@test-are_identical_posteriors.R#58) 
  6. Error: do_simulation: use (@test-do_simulation.R#22) 
  7. Error: do_test_simulations: create exact replicate (@test-do_test_simulations.R#9) …
Run Code Online (Sandbox Code Playgroud)

logging r travis-ci testthat

5
推荐指数
0
解决办法
126
查看次数

如何用R testthat生成jUnit fie

我有R代码(不是包),我想用验证测试覆盖验证测试,在Jenkins中使用输出.

我可以从两个演示代码结构的文件开始:

# -- test.R
source("test-mulitplication.R")

# -- test-mulitplication.R
library(testthat)
test_that("Multipilation works ", {
  res <- 5 * 2
  expect_equal(res, 10)
})
Run Code Online (Sandbox Code Playgroud)

运行后,我想获得一个xml文件,其中包含每个测试文件的结果或单个文件中的所有测试.

我注意到reportertestthat中有一个功能,但大部分功能似乎都在内部.目前尚不清楚如何保存测试结果以及功能的灵活性.

不幸的是,该部分的文档并不完整.

编辑

我现在找到了一种方法来测试目录,使用更好的语法和junit输出选项:

# -- tests/accpetance-tests.R
options(testthat.junit.output_file = "test-out.xml")
test_dir("tests/")

# -- tests/test-mulitplication.R
library(testthat)
test_that("Multipilation works ", {
  res <- 5 * 2
  expect_equal(res, 10)
})
Run Code Online (Sandbox Code Playgroud)

我相信这会在记者中产生一个XML对象,但我仍然没有看到如何将它保存到文件中.

我试着test_dir打电话with_reporter,但这并没有多大作用.

junit r jenkins testthat

5
推荐指数
1
解决办法
1007
查看次数

如何测试数据框内容?

我有一个返回数据帧的例程。我想检查其值是否合理。使用testthatI have expect_equal,但是我不确定它是否适用于data.frames。我试图这样做,但是没有用

testthat::expect_equal(result$ORs[1,1:3], c(1.114308, 0.5406599, 2.296604), tolerance=1.0e-6)
Run Code Online (Sandbox Code Playgroud)

这是我收到的消息

?????????????????????????????????????????????????
test-xxx.R:19: failure: basic functionality
result$ORs[1, 1:3] not equal to c(1.114308, 0.5406599, 2.296604).
Modes: list, numeric
names for target but not for current
Attributes: < Modes: list, NULL >
Attributes: < Lengths: 2, 0 >
Attributes: < names for target but not for current >
Attributes: < current is not list-like >
?????????????????????????????????????????????????

?? Results ??????????????????????????????????????
Duration: 0.1 s
Run Code Online (Sandbox Code Playgroud)

r testthat

5
推荐指数
1
解决办法
57
查看次数

如何模拟rest API的http响应?

我正在寻找在testthat框架内模拟其余 API 响应的最简单方法。

用法示例与此类似:

with_mock_api(
  request, 
  response, 
  {
      call_to_function_with_api_call()
      # expectations check
  }
)
Run Code Online (Sandbox Code Playgroud)

因此,测试将通过而不调用真正的 API。

  • request 指将在 api 包装函数内部完成的 http 请求的定义;
  • response 指为了模拟而缓存的响应对象。

api r http mocking testthat

5
推荐指数
1
解决办法
320
查看次数

标签 统计

r ×10

testthat ×10

api ×1

devtools ×1

http ×1

jenkins ×1

junit ×1

logging ×1

mocking ×1

travis-ci ×1