My question is more to do with structure/cleanliness. I have a test script in TestNG and am looking to add test cases to expand. I want to know if there is a clean way to repeat test cases. Example :
I can do :
@Test(priority=0, name = login )
//some code
@Test(priority=2, name = submit )
//some code
@Test(priority=3, name = accept )
That is just one test scenario. Now I want to do the same thing for a different flow but the test case is repeating ... example:
@Test(priority=0, name = login )
//some code
@Test(priority=2, name = submit )
//some code
@Test(priority=3, name = rejected )
In both flows, 2 tests are the same where I am repeating login and submit. The only change in flow would be what happens each time after submission, such as Submit, Rejected, Cancelled etc. etc. I only know of one way, and that is to repeat writing the login and submit each time.
The question is, is there better/cleaner more structured way to write this instead of the below where I keep repeating login every time?
@Test(priority=0, name = login )
//some code
@Test(priority=2, name = submit )
//some code
@Test(priority=3, name = accept )
//Some code
@Test(priority=4, name = login )
//some code
@Test(priority=5, name = submit )
//some code
@Test(priority=6, name = rejected )
//some code
.
.
.
@Test(priority=n ...
If your goal is to remove duplication of tests, how about creating more invidualized tests and running them multiple times by specifying so in a testng.xml file ?
For example, you could do:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test Suite 1">
<test name="Test Set 1">
<classes>
<class name="test.mytest.Test1" />
<class name="test.mytest.Test2" />
</classes>
</test>
<test name="Test Set 2">
<classes>
<class name="test.mytest.Test1" />
<class name="test.mytest.Test3" />
</classes>
</test>
</suite>
With your classes set up as:
public class Test1 {
@BeforeMethod
public void before(){
System.out.println("==Before method==");
}
@Test
public void testLogin(){
System.out.println("test login");
}
@Test
public void testSubmit(){
System.out.println("test submit");
}
}
public class Test2 {
@Test
public void testAccept(){
System.out.println("test accept");
}
}
public class Test3 {
@Test
public void testReject(){
System.out.println("test reject");
}
}
When run(I used Maven in my example), will produce:
Running TestSuite
==Before method==
test login
==Before method==
test submit
test accept
==Before method==
test login
==Before method==
test submit
test reject
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.253 sec - in TestSuite
For this example I organized them by class, but you could organize by group, method, etc.
EDIT Based on your comment I added an example of using @BeforeMethod and the output produced.
If you are looking to specifically reuse your @BeforeMethod, I suppose you could always just throw it into a base class and have your test cases extend it:
public class TestBase {
@BeforeMethod
public void before(){
System.out.println("==Before method==");
}
}
public class Test1 extends TestBase{
@Test
public void testLogin(){
System.out.println("test login");
}
@Test
public void testSubmit(){
System.out.println("test submit");
}
}
I've never needed to do something like that, so I can't speak to it in terms of best practices, but it would work.
Will this work ?
public class BeforeStuff {
@BeforeMethod()
}
public class Test1 {
@Test
@Test
}
public class Test2 {
@Test
}
public class Test3 {
@Test
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.