如何对具有多个用户输入的交互功能进行单元测试?

问题描述 投票:0回答:0

我有几个函数,我试图将它们包含在一个包中,这些函数使用 scan() 请求用户输入。通过使用 ZNK 的 answer,我想出了如何使用 testthat 包对一个函数进行单元测试,仅针对一个用户输入。我想尝试将其扩展到具有多个用户输入的函数,但我一直在研究如何创建多个连接并在函数中引用它们。

这是一个复制的例子:

# Simulate households with children
subpop_children <- function(df, n = 5){

  
  # user input
  cat("please enter a number here")
  num1 <- scan(n = 1, what = double())
  

  cat("Please enter three numbers separated by space:")
  num2 <- scan(n = 3, what = double())
  
  cat("Please enter three more numbers separated by space:")  
  num3 <- scan(n = 3, what = double())
  

  cat("please enter a number")
  num4 <- scan(n = 1, what = double())

  return(rep(num2 + num3, num1+num4))

}

如果可能的话,我如何使用 testthat 库对上述函数进行单元测试?

避免用户输入并简单地将它们转换为函数的参数会更好吗?提前谢谢你。

r devtools r-package testthat
© www.soinside.com 2019 - 2024. All rights reserved.