0

是否可以将一个 JUnit 测试的结果/输出与同一类中的另一个测试进行比较?

下面是算法

Public class CompareResult {

@Before
{
open driver
}

@After
{
quit driver
}

@Test
{
Connect to 1st website
Enter data and calculate value
Store the value in Variable A
}


@Test
{
Connect to 2nd website
Enter data and calculate value
Store the value in Variable B
}

@Test
{
Compare A and B
}
}

当我在第三个@Test 中显示变量 A 和 B 的值时,它为 NULL。我们不能在一个@Test 中将变量用于JUnit 中的另一个@Test 吗?请告知,我是 JUnit 的新手。

4

2 回答 2

2

为什么他们需要进行两次测试?如果您正在比较这些值,那么您确实有一个包含多个方法和可能多个断言的测试。如果 helper1 和 helper2 中没有任何断言,这将变得更加明显。没有断言的测试只是测试它不会爆炸!

private helper1
{
    // Connect to 1st website
    // Enter data and calculate value
    // Store the value in Variable A
}


private helper2
{
    // Connect to 2nd website
    // Enter data and calculate value
    // Store the value in Variable B
}

@Test actualTest
{
    // Compare A and B with assertion
}
于 2012-04-09T00:33:14.913 回答
0

正如我所理解的,您将值存储在局部变量中。首先声明私有字段 A 和 B,然后使用它来存储您的数据。

Public class CompareResult {

    private String a = null;
    private String b = null;

    @Before
    public void Setup() {
        open driver
    }
    ...

顺便说一句,您的测试应该是独立的,并且将值从一个测试传递到另一个测试并不是实现它们的好方法。另外我没有经常使用junit,所以我不知道你的测试的执行顺序是如何设置的。你应该定义一些测试依赖或类似的东西,我再重复一遍:这对于测试是不正确的。

于 2012-04-05T12:43:51.057 回答