java


How to get #Test methods from the test classes, when running TestNG programmatically


I'm creating an application to programmatically execute our java webdriver selenium scripts. We use TestNG.
I'm able to launch the classes with a virtual XML file. This works:
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("browser", "Firefox");
suite.setParameters(parameters);
classes.add(new XmlClass("package.classname"));
test.setXmlClasses(classes) ;
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
Since the above method uses the package.classname to execute the class, I need a way to get all our classes; both their package and their class name (We have hundreds of packages).
I use this code to grab all the package.classnames. In simply scans the /bin/ directory of the project, converting the folder structure into package names and removes the .class extension from the class file. This works, too:
//scans the /bin directory of project to grab all packages/classes
String binPath = "V:\\Selenium\\Scripts\\bin\\";
File dir = new File(binPath);
List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
List<String> classNames = new ArrayList<String>(300);
for (File f : files) {
//instead of "V:\Selenium\Scripts\bin\package\classname.class"
//I just end up with "package.classname"
classNames.add(extractClassNameFromPath(f.getPath()));
}
So at this point, I'm able to get a String list of all my class names, and I can put any of them into the classes.add(new XmlClass("package.classname")); line, allowing me run any of our test classes programmatically. Great!
With all that said, here is the problem I'm facing now:
The classes have different #Test methods. By launching just the class, all of the #Test methods will be executed. I want to be able to extract the available #Test methods in that class and present them to the user so they can choose which ones they want.
I have no idea how to extract the #Test methods from our test classes.
You can use reflections library and its MethodAnnotationsScanner:
Reflections reflections = new Reflections("my.package");
Set<Method> resources =
reflections.getMethodsAnnotatedWith(org.testng.Test.class);

Related Links

Regarding initialization of class instances
Sending events via code generation with annotations
Automatically generate Retrofit Types
ClassNotFoundException Error Spring + JSF trying to implement Spring Security login
JPA Entity updating database without persist/merge
Android Studio can't find installed SDK packages
Java SOAP client stub generation with service endpoint determined at runtime
Java EE Websocket: How to send keep sending data without receiving message?
Jetty 9.0.6 - java.net.ConnectException: Connection refused: connect
how to export Javassist CtClass to file
Grails create-app error-nothing works out
Setting member variable in a private method or outside?
Javadoc for the element could not be found
AdMob taking space in layout, but no ad is actually appearing?
Rearrange a table of values
How to decide the application name before launching to PlayStore

Categories

HOME
google-chrome-extension
clips
reflection
pycharm
react-redux
jxls
binary-tree
in-app-purchase
swagger-ui
esper
amazon-cloudformation
tomcat6
remote-access
windows-server-2012
lombok
dosgi
evopdf
beyondcompare
shared-hosting
introduction
immutable.js
sox
tapestry
google-search-api
rundeck
smb
django-storage
greendao
typo3-6.2.x
replaceall
gsmcomm
hexo
bytecode-manipulation
ios5
xcode-extension
x11-forwarding
neuroscience
libraries
withings
uft-api
hue
broadcastreceiver
mapzen
return-value
colorama
setter
diagnostics
dtexec
namecoin
httplistener
rotativa
radtreelist
dstu2-fhir
simplewebrtc
prettytensor
packagist
javax.sound.midi
kendonumerictextbox
muse
vstest.console.exe
nsight
p2
jsapi
lustre
python-green
formatjs
nstableviewcell
id3v2
openexr
heisenbug
htmlcleaner
resty-gwt
concurrent-collections
gnu-smalltalk
enterprisedb
jelly
dmoz
frameset
pysimplesoap
pendrive
punbb
hgsubversion
cufon
vdsp
horizontal-accordion
netbeans-7.1
locate
yslow
dmx512
gin
photoshop-cs4
exchange-server-2003
noscript

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App