score:0
will this work ?
public class beforestuff {
@beforemethod()
}
public class test1 {
@test
@test
}
public class test2 {
@test
}
public class test3 {
@test
}
}
score:1
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.
Source: stackoverflow.com
Related Query
- Is there are cleaner way to Structure your TestNG test cases?
- Is there a way to test a jar file of my project with junit test cases using CLI(command line interface)?
- What is the difference between TestNG and Jenkins? Are both used to run Selenium test cases and two similar options?
- Is there a way to make Eclipse run a JUnit test multiple times until failure?
- Is there a way to test android applications directly on phone, without the emulator?
- Is there a way to make Eclipse continually refresh your package-explorer files?
- Is there a way in Eclipse to find out plugins that are not used?
- Is there a way to use a custom JUnit Test Runner when running tests in Eclipse?
- Is there a way to get version information rather than "(without test code)" in Eclipse Project Explorer's Maven dependencies?
- Is there a way to make eclipse detect your custom view as its parent layout?
- Debugging test cases when they are combination of Robot framework and python selenium
- Is there a way to see what commands are executed by eclipse?
- Run testNG tests based on "priority" of test cases
- Is there a quicker way to test android apps?
- TestNG test result console is blank only if I run the test cases using Maven(Test)
- Is there a way to make Junit3 the default Test Runner in Eclipse 3.4.0?
- How do can i display all test cases of TestNG upfront to choose some for execution - test explorer?
- Is there a way to indicate if we are exceeding a certain size limit on certain fields
- is there a way to list symbols that are defined in c/c++ by #define
- how to repeat a particular Test Method in TestNG which are depent on an other Test Method
- is there a way to run the java programs that are developed in netbeans in eclipse?
- Not able to run Test cases in TestNG
- Is there a way I can develop and test Map reduce program on windows development box
- Is there an alternative way to create Junit Test results report instead of ant's Junitreport task
- Is there a way to test the loss of GPS provider on Android? (onProviderDisabled())
- When TestNG suite is executed, older versions of test class are being used
- JUnit test cases not displayed in JMtere when create a tests in simple class file but are displayed when create for JUnit class
- How should I write TestNG test cases so that they can be run one after another (sequentially)?
- There isn't any info about my test execution on the resulting tab of TestNG Eclipse plugin
- Is there a way to watch current Junit test events?
More Query from same tag
- Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONArray
- eclipse rcp , the packageyyy is accessible from more than one module: <unnamed>
- PDFBox Embeded fonts not working when filling form
- How to read opened file and display content in Eclipse Master Details block?
- Egit fetch & pull connection refused
- my test application of worklight for android stopped unexpectedly when i tried to run that on AVD?
- is maven required to develop spring based project?
- JTDS error: Native SSPI library not loaded
- How to use javassist to change the eclipse's compilation?
- Eclipse Error On Startup
- Eclipse IDE Redo and Inspect key command Conflicts
- Eclipse M2E Plugin don't add dependency from same workspace
- Equals button in calculator doing nothing (Android Developer Tools)
- Unable to simulate location data in Android emulator
- JavaDoc Tags in Eclipse
- Eclipse Javascript Plugin extension
- How to add new Fields/Elements to JsonObject?
- 'org.eclipse.platform.feature.group [3.4.0,4.0.0)' could not be found
- How can I do visual development in Eclipse using JSF's Trinidad UI library?
- can't find referenced method 'android.app.RemoteInput[] getRemoteInputs()' in class android.app.Notification$Action
- Eclipse plugin to find the error on Double Click the file
- Resource leak: 'in' is never closed
- How to use SQLJ with Eclipse?
- Getting "new" dependency from org.eclipse.xtext.common.types
- Can't import any referenced libraries from build path in eclipse
- How separate method calls with eclipse?
- enabling https in eclipse tomcat server not allowing calls to port 8080
- force eclipse to use Python 3.5 autocompletion
- Tensorflow GPU Python 3.5. Eclipse has error: ImportError: libcudart.so.8.0: cannot open shared object file:
- How to fix : http://URL: not authorized error in eclipse while cloning project from GIT?