R:test_that多个对象相等

时间:2015-04-13 10:01:56

标签: r testing

我试图在脚本中使用testthat::test_that包含一个测试,并希望编写一个多个对象相等的测试:

dfX1 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX2 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX3 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

# something like this?
stopifnot(all(row.names(dfX1) == row.names(dfX2) == row.names(dfX3)))

我完全清楚最后一行毫无意义。我正在寻找一个符合testthat语义的紧凑解决方案。

2 个答案:

答案 0 :(得分:1)

试试这个:

dfX1 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX2 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX3 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

# something like this?
stopifnot(identical(union(row.names(dfX1) ,row.names(dfX2)),
                row.names(dfX3)) == TRUE)

超过2个载体使用:

stopifnot(identical(Reduce(union, list(row.names(dfX1) ,row.names(dfX2),
row.names(dfX3, ... , row.names(dfX_Nminus1))),
                row.names(dfX_N)) == TRUE)

这也有效,因为所有向量的长度相等:

l.d <- list(row.names(dfX1) ,row.names(dfX2),
        row.names(dfX3))
stopifnot(length(Reduce(intersect, l.d)) == length(l.d[[1]]))

答案 1 :(得分:0)

测试的两个解决方案:

library(testthat);library(purrr)
# example data
dfX1 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))
dfX2 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))
dfX3 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

测试解决方案1 ​​ - 不能确定问题所在,但是是单行

expect_equal(unlist(unique(map(list(dfX1, dfX2, dfX3), row.names))), row.names(dfX1))

有错误

olddfX3<-dfX3
row.names(dfX3) <- paste0(row.names(dfX3), 1)

expect_equal(unlist(unique(map(list(dfX1, dfX2, dfX3), row.names))), row.names(dfX1))
# Error: unlist(unique(map(list(dfX1, dfX2, dfX3), row.names))) not equal to row.names(dfX1).
# Lengths differ: 20 vs 10

dfX3<-olddfX3

测试解决方案2 - 2x2测试 - 会给你很长的错误文本,但你可以找出问题

tests_2x2 <- function(names){

  lss <- setNames(lapply(names,get),names)

  len <- length(lss)


  for(x in 1:len){
    y=x+1
    while(y<=len){
      test_that(paste0("test that row.names(", 
        names(lss)[x], ") == row.names(", names(lss)[y], ")"), {
          expect_equal(row.names(lss[[x]]), row.names(lss[[y]]))})
      y=y+1
    }
  }
}

输入df名称以获得测试的特定结果

names<-c("dfX1", "dfX2", "dfX3")
tests_2x2(names)

一个有错误:

olddfX3<-dfX3
row.names(dfX3) <- paste0(row.names(dfX3), 1)
tests_2x2(names)
# Error: Test failed: 'test that row.names(dfX2) == row.names(dfX3)'
# * row.names(lss[[i]]) not equal to row.names(lss[[i + 1]]).
# 10/10 mismatches
# x[1]: "team1"
# y[1]: "team11"
# 
# x[2]: "team2"
# y[2]: "team21"
# 
# x[3]: "team3"
# y[3]: "team31"
# 
# x[4]: "team4"
# y[4]: "team41"
# 
# x[5]: "team5"
# y[5]: "team51"
# 
# x[6]: "team1"
# y[6]: "team11"
# 
# x[7]: "team2"
# y[7]: "team21"
# 
# x[8]: "team3"
# y[8]: "team31"
# 
# x[9]: "team4"
# y[9]: "team41"
# 
# x[10]: "team5"
# y[10]: "team51"