There are 2 files outside of the application (A.class and ATest.class).
How can we scan ATest to find all junit tests and run them (assuming the dependencies of ATest are loaded in classpath except A)? I'm looking for doing this programmatically in Java and as a result having the list of the tests of ATest, which failed and which succeeded.
The command line must not be used (in case of we need a more complex output).
Thanks for your help.
Related
How does TestNG work when it is looking for its test classes?
I created a separate project from my TestNG project using JavaFX to create a GUI, when the GUI is run it enables you to select an XML file and then the path to the testNG XML is saved in a variable.
When I run the following code:
String xmlFileName = selectedFile.getAbsolutePath();
TestNG testng = new TestNG();
List<String> testFilesList = new ArrayList<String>();
testFilesList.add(xmlFileName); //test suite resides in the working directory's root folder
testng.setTestSuites(testFilesList); //you can addd multiple suites either here by adding multiple files or include all suites needed in the testng.xml file
testng.setUseDefaultListeners(false);
testng.addListener(htmlRep);
testng.run();
I get an error saying that it cannot find my test classes inside the XML.
[TestNG] [ERROR]
Cannot find class in classpath: com.emc.qe.u360.tests.LogoutTests
I then decided to make a java class within my TestNG project, and copy the code over and it still gives the same error message.
Where is TestNG looking for the test classes when the above code is run?
What difference is the code above from manually running the XML from eclipse itself? The code seems to be working, as from what I can see if the code for running the XML was broken or not working, it wouldn't be able to give me that error as it wouldn't be able to determine what is specified in the XML.
Any help would be appreciated, thank you.
EDIT: Just for background information, currently I am using Jenkins to run the tests, what I'm trying to do with the GUI is to create an alternative solution to running the tests outside of the Project/Framework, that can be used universally, a user can just select their XML and then the tests will be executed.
It's configurable if you use the Maven Surefire or the Gradle TestNG runner, but by default I think TestNG looks in src/test/java for your class names. Additionally, I think if your running TestNG from code, as a standalone run, you might be able to adjust the path-base where it looks for classes.
I have a project that itself is a set of test cases for an application. The test cases are tested itself using a mockup of the application to ensure the tests itself is correct. This is important to ensure the test cases justify some sort of specifications that are hard to follow and easy to mess up.
Now I want the final tests (those who are tested before) being in src in Maven. As expected mvn test just executes the test cases of the test cases not the test cases themself.
So basically how do I execute test cases that reside inside the 'src' folder using a maven goal?
Try this :
<testSourceDirectory>src</testSourceDirectory>
How can I find tests that have not been added to a test suite?
If I run the test suite I get say 1000 tests but if I run a file search for "#Test" I get 1020 results. How can I go and add the missing ones without having to go through every search result and comparing to the tests that ran to see which ones are missing?
Use Eclipse itself to create a test suite. From the context menu of the project, select 'New -> Other' and select JUnit test suite. It will list out all the test classes in the project. By default, it creates a AllTests.java source file.
I'm having trouble running my JUnit test, it fails because it is not able to find the projects resources.
My test starts a server which loads all the needed resources using an URLClassLoader. I have no problem with the resources not being found when running my project otherwise. This problem only occurs when I try to run my JUnit test.
I have tried adding the file paths as arguments in my runtime configuration for the test like this, ex:
-cp .:/path/to/the/config/file
But it makes no difference.
Help please!
Project structure:
Default Maven test path is src/test/java, so if your tests are not in that directory, you have to define your test path in pom.xml with tag <testSourceDirectory>, inside <build>. You have to include maven-surefire-plugin, too.
The convention in Maven is to have all Java test code in src/test/java. This is to prevent accidental inclusion of test code in the final product. So I suggest you move your tests.
Since you don't show the code which you use to load the resource, I can't tell you whether there are any bugs in that. But /path/to/the/config/file is most likely wrong; is must be /path/to/the/config so you can say in Java getClass().getClassLoader().getResource("file").
But I suggest to use a command line argument to specify the config file:
main(String[] args) {
File configFile = new File(args[0]).getAbsoluteFile();
if(!configFile.exists()) throw new IllegalArgumentException("Unable to find config file: " + configFile);
...
}
That makes error handling much more simple. Also, it allows to use different config files in tests and later in production.
I have an issue with some gradle task that should run tests. Since this is some legacy from ant tasks we do not want to include them into our test suite. Especially considering that those ant ones are in testng, and those made by us, and used on regular basis are made using spock and junit.
The problem is that those tests are using some context which works pretty well when I run those tests under eclipse IDE, but it fails if I try to do sth like:
task testNgTesting(type: Test, dependsOn: testClasses){
useTestNG()
includes = ["**/*IT*"]
}
But when I try to use that task I get errors like "org.hibernate.MappingException: Unknown entity:" or "java.lang.IllegalArgumentException: No query defined for that name"
Actually the problem is deeper. Gradle is trying to be smart and from whatever folder it has defined it puts classes files into classes folder, and all the other files into resources. When persistence.xml is loaded it scans for annotated entities starting with classpath root for folder it is present in (i.e. build/resources/main). Since all those classes are in build/classes/main it fails to find them. The workaround I've made is introduce two copy tasks. one named is copying persistence.xml into classes folder, and another is moving this file back to resources after the tests are finished. You might want to use something like
testNgTesting.finalizedBy(cleanAfterIntegrationTests) to make sure the cleanup occurs even if there are some tests that fails.