Java 什么是测试预言机,它的用途是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23522166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:33:46  来源:igfitidea点击:

What is a test oracle and what is it used for?

javaunit-testingtestingjunit

提问by user1170330

I don't really understand the concept of a software testing oracle. It says:

我不太了解软件测试 oracle的概念。它说:

An oracle is a mechanism for determining whether the program has passed or failed a test.

oracle 是一种用于确定程序是否通过测试的机制。

Consider the following code:

考虑以下代码:

// class that should be tested
public int sum(int a, int b) {
  return a + b;
}

// test class
static Main tester = new Main();
@Test
public void testSum() {
  assertEquals("2 + 3 is 5", 5, tester.sum(2, 3));
}

The class that should be tested always returns the sum of 2 integers.
I pass as a parameter 2 and 3, and expect 5. 2 and 3 will be summed up and compared to the expected value (5). In this case the test succeeds.

应该测试的类总是返回 2 个整数的总和。
我作为参数 2 和 3 传递,期望为 5。2 和 3 将相加并与期望值 (5) 进行比较。在这种情况下,测试成功。

How exactly can an oracle help me here? Is an oracle involved in this example?

甲骨文究竟如何帮助我?这个例子中是否涉及到一个预言机?

采纳答案by Dave Schweisguth

A test oracle is a source of information about whether the output of a program (or function or method) is correct or not.

测试预言机是关于程序(或函数或方法)的输出是否正确的信息来源。

A test oracle might specify correct output for all possible input or only for specific input. It might not specify actual output values but only constraints on them.

测试预言机可能会为所有可能的输入或仅为特定输入指定正确的输出。它可能不会指定实际的输出值,而只是对它们的约束。

The oracle might be

神谕可能是

  • a program (separate from the system under test) which takes the same input and produces the same output
  • documentation that gives specific correct outputs for specific given inputs
  • a documented algorithm that a human could use to calculate correct outputs for given inputs
  • a human domain expert who can somehow look at the output and tell whether it is correct
  • or any other way of telling that output is correct.
  • 一个程序(与被测系统分开),它接受相同的输入并产生相同的输出
  • 为特定给定输入提供特定正确输出的文档
  • 人类可以用来计算给定输入的正确输出的记录算法
  • 可以以某种方式查看输出并判断其是否正确的人类领域专家
  • 或任何其他方式告诉输出是正确的。

If not vague, the concept is at least very broad.

如果不模糊,这个概念至少是非常广泛的。

An oracle isn't a test runner, but a test runner could use an oracle as a source of correct output to which to compare the system-under-test's output, or as a source of constraints against which to evaluate the SUT's output.

oracle 不是测试运行器,但测试运行器可以使用 oracle 作为正确输出的来源,以便与被测系统的输出进行比较,或者作为评估 SUT 输出的约束来源。

In your example, you used your personal ability to carry out the algorithm of addition as your oracle. Instead, you could use a different implementation of that algorithm as an oracle:

在您的示例中,您使用个人能力来执行加法算法作为您的预言机。相反,您可以使用该算法的不同实现作为预言机:

assertEquals("2 + 3 is 5", 2 + 3, tester.sum(2, 3));

回答by Paul

Let me pose the oracle question this way: how can we check that the program returns the right answer?

让我以这种方式提出预言机问题:我们如何检查程序是否返回了正确的答案?

For this function, we can easily check the answer with the following pseudocode (Sorry, it's not C++.):

对于这个函数,我们可以很容易地用下面的伪代码检查答案(对不起,它不是 C++。):

repeat many times {
    int a = randomNumber();
    int b = randomNumber();
    int result = sum(a, b);
    assertEquals("random case", a, result - b);
}

This oracle uses subtraction to check the function. This allows millions or billions of tests to be run with little human effort.

这个oracle使用减法来检查函数。这允许以很少的人力运行数百万或数十亿次测试。