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

java.lang.IllegalStateException while moving actionbar to toolbar in android
theme layering codename one
Java regex to groovy
How to enable monitor mode and run airodump-ng in Java?
OWLAPI: Parser not found if run from Jar
Maven creating two jars from flink project and only 80mb one works in cluster
XDocReport : ODT generated with wrong values
How to refer to another bean as a property in annotation-based configuration file
Retrieve and display columns and data in a MySQL table in a particular JSON structure
How can I make an Parallel AsyncTask or a method that reacts to a change in a variable in android?
spring batch Semi-written records count
Why is this GoLang solution faster then the equivalent Java Solution?
Code will only return 0.0, 0.0 GPS coordinate while throwing NullPointerException
Android studio, adding button to spinner layout stops the spinner from working
Java NoSuchMethodError when Method Exists
Android multiple language support to dynamic content

Categories

HOME
mediawiki
angular-material
react-router
webstorm
spagobi
analysis
ezpublish
azure-media-services
applepay
slurm
rascal
modelica
zebra-printers
iron-router
tostring
invantive-sql
ab-testing
immutable.js
wkwebview
buck
libssl
objectanimator
c++-amp
newline
skia
xquery-3.0
copying
textmate
hexo
http-digest
revolution-slider
space-complexity
starteam
reportbuilder
hue
temporary-files
azure-ml
turbogears
total-commander
apple-news
scrollable
parentheses
filepicker
apache-fop
jspdf-autotable
spring-cache
hittest
account-kit
topbeat
medium.com
visual-c++-2008
grails-tomcat-plugin
qpid
websitepanel
lttng
markojs
persist
ready-api
whois
php-parse-error
design-by-contract
itextpdf
cloudbees
jfugue
endeca-workbench
libressl
nsight
unity5.2.3
sonarqube5.1.2
umbraco6
facebook-graph-api-v2.4
network-interface
bitcoinj
wp-query
reactfx
terminal-services
ms-project-server-2010
cdc
dotnetnuke-5
padarn
sqlperformance
pygit2
delphi-6
coderush
pysimplesoap
viewswitcher
parametric-equations
assembly-loading
libc++
gdlib
revisions
javap
swing-app-framework
noscript
mediarss
multiple-languages

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