问题
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