Comparing result of one JUnit @Test with another @Test in same class

时光怂恿深爱的人放手 提交于 2020-01-06 14:32:10

问题


Is it possible to compare the result / output of one JUnit test to another test in same class?

Below is the algorithm of

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
}
}

When I display the value of variable A & B in 3rd @Test, it is NULL. Can we not use variable in one @Test to another @Test in JUnit? Please advise, I am new to JUnit.


回答1:


Why do they need to be two tests? If you are comparing the values, you really have one test with multiple methods and possibly multiple asserts. And if there aren't any asserts in helper1 and helper2, this becomes even more apparent. A test without an assert is just testing it doesn't blow up!

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
}



回答2:


You store your values in local variables as I understood. Declare private fields A and B first and then use it to store your data.

Public class CompareResult {

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

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

Btw your tests should be independent and passing values from one test to another is not a good way to implement them. Also I didn't work with junit a lot so I don't know how execution order for your tests is set. You should define some tests dependency or something like that and I repeat it once again: this is not correct for tests.



来源:https://stackoverflow.com/questions/10019319/comparing-result-of-one-junit-test-with-another-test-in-same-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!