找出测试失败的原因

Working out why a test failed

我正在尝试使用 Postman 来测试 API 我正在使用 express 构建,在其中我正在构建一套测试。这是一个简单的例子:

tests["Status code is 200"] = responseCode.code === 200;

// Check that we got the expected board
var expected = { _id: 11, name: "board A" };
tests["Board retrieved"] = JSON.stringify(expected) === responseBody;

然而,我发现当我的测试可能失败时,他们没有告诉我任何有用的信息:

是否有不同的方法来验证结果,让我知道 expected/actual 值,就像任何人都知道的传统测试运行器一样?到目前为止我唯一能想到的就是将信息嵌入到测试名称中——感觉有点笨拙。

可能的解决方法是包装分配以添加更多功能:

/** This function adds information in the label only if the validations returns false */
tests.assertEquals= function (expected, actual, label) {
  if (expected!==actual) {
    this[label + ': Expected "' +expected + '" but got "'+ actual +'"']=false;

  } else {
    this[label]=true;
  }
}

tests.assertEquals(JSON.stringify(expected), responseBody,"Board retrieved");

正如您所说,这可能是一个问题,但您只有在必要时才拥有此信息,并且使用一种方法来保存它 'hidden' 可以使它更清晰、更易于阅读。另一种选择是使用 console.log 来显示额外信息而不是将其添加到标签中,但这只是个人喜好问题