【问题标题】:knitr: include test that on purpose fails in output PDFknitr:在输出 PDF 中包含故意失败的测试
【发布时间】:2017-07-11 00:43:07
【问题描述】:

我想在失败的 Rmd 文件中添加测试(不是测试 Rmd 文件,而是在文档中包含测试),但我无法使用 knitr 生成 pdf,因为它会在测试失败时停止。如何做到这一点?代码:

---
title: "test"
output:
  pdf_document: default
  html_document: default
---

```{r, error=TRUE}
    library(testthat)
    expect_equal(1, 2)
```

```{r, error=TRUE}
    library(testthat)
    expect_equal(1, 1)
```

环境:

R version 3.3.1 (2016-06-21)
Platform: x86_64-redhat-linux-gnu (64-bit)
[...]
loaded via a namespace (and not attached):
 [1] backports_1.0.5 magrittr_1.5    rprojroot_1.2   htmltools_0.3.5 tools_3.3.1     yaml_2.1.14     Rcpp_0.12.9    
 [8] stringi_1.1.2   rmarkdown_1.3   knitr_1.15.1    stringr_1.2.0   digest_0.6.12   evaluate_0.10  

【问题讨论】:

    标签: r knitr r-markdown


    【解决方案1】:

    解决方案是使用test_that。以下代码完美运行:

    ```{r, error=TRUE}
        library(testthat)
        test_that(1, expect_equal(1, 2))
    ```
    
    ```{r, error=TRUE}
        test_that(2, expect_equal(1, 1))
    ```
    

    请注意,test_that 中的第一个参数是测试的名称。

    【讨论】:

    • 看来以前的格式适用于 testthat_0.11.0 和最新的 lib 你的方法是必要的。谢谢
    猜你喜欢
    • 2018-05-06
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2015-03-25
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多