“ testthat”测试在会话中运行时确实会运行,但会导致测试错误

时间:2019-03-28 09:25:35

标签: r testthat

我在下面的testthat测试中遇到了问题。特别是第一次测试。

它在我直接执行它时运行(整个test_that()块),但是在该文件上使用testthat::test_file()时它失败了(很显然,在全部执行测试的情况下)。来自

context("validate()")

x <- emeScheme_raw
print(validate( x = x, errorIfStructFalse = TRUE))

test_that(
  "validata_raw() returns correct value when correct",
  {
    expect_known_value(
      object = validate( x = x, errorIfStructFalse = TRUE),
      file = "validate.CORRECT.rda"
    )
  }
)

names(x)[1] <- "experiment"

test_that(
  "validata_raw() fails",
  {
    expect_known_value(
      object =  validate( x = x, errorIfStructFalse = FALSE),
      file = "validate.DIFFERENCES.rda"
    )
  }
)

test_that(
  "validata_raw() fails",
  {
    expect_error(
      object = validate( x = x, errorIfStructFalse = TRUE),
      regexp = ("Structure of the object to be evaluated is wrong. See the info above for details.")
    )
  }
)

结果

 testthat::test_file("./tests/testthat/test-validate.R")
✔ | OK F W S | Context
⠙ |  1 1     | validate()Names: 1 string mismatch
✖ |  2 1     | validate() [0.8 s]
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
test-validate.R:10: error: validata_raw() returns correct value when correct
missing value where TRUE/FALSE needed
1: expect_known_value(object = validate(x = x, errorIfStructFalse = TRUE), file = "validate.CORRECT.rda") at ./tests/testthat/test-validate.R:10
2: compare(act$val, ref_val, ...)
3: compare.default(act$val, ref_val, ...)
4: all.equal(x, y, ...)
5: all.equal.list(x, y, ...)
6: all.equal(target[[i]], current[[i]], check.attributes = check.attributes, use.names = use.names, ...)
7: all.equal.list(target[[i]], current[[i]], check.attributes = check.attributes, use.names = use.names, ...)
8: all.equal(target[[i]], current[[i]], check.attributes = check.attributes, use.names = use.names, ...)
9: all.equal.list(target[[i]], current[[i]], check.attributes = check.attributes, use.names = use.names, ...)
10: all.equal(target[[i]], current[[i]], check.attributes = check.attributes, use.names = use.names, ...)
11: all.equal.default(target[[i]], current[[i]], check.attributes = check.attributes, use.names = use.names, ...)
12: all.equal.list(target, current, ...)
13: paste0("Component ", if (use.names && nt[i] == nc[i]) dQuote(nt[i]) else i, ": ", mi)
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

══ Results ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Duration: 0.8 s

OK:       2
Failed:   1
Warnings: 0
Skipped:  0

如果我正确读取了错误,则该错误发生在expect_known_value()函数中,而不是在有意义的validate()函数中,因为该函数可以正常工作(如果我将其放在{{1}之后}分配,它将成功运行并返回结果。

我知道没有可复制的示例很难做到这一点,但是有什么想法吗?

0 个答案:

没有答案