I am writing several different selenium tests as page objects, and want to be able to run them from within a single, central class. I have figured out how to run one test from a different class, but when I try to run multiple tests, only a single one will complete.
I have tried running them sequentially with org.junit.runner.JUnitCore.main("com.etc"), but after
testing the first class, the entire test ends. In the code:
org.junit.runner.JUnitCore.main("com.etc.test.HomePageCheck");
System.out.print("test");
the print command is never run, even if the test runs successfully
I have also tried creating multiple threads, but once any test completes, the whole process seems to end and leave the remaining tests hanging.
To reiterate, I have Class1 with several jUnit tests, and Class2 with other jUnit tests. I want to be able to run a Class3, whcih will run both Class1 and Class2 and complete all tests for those other classes.
I do not think I want to use Selenium Grid, I would rather just run the tests locally on a machine, either in sequence or, preferably, in parallel.
I figured it out, here is the code(second line is key, the others are just to get the output)
Result aTest;
aTest= org.junit.runner.JUnitCore.runClasses(new Class<?>[]
{
Events.class,SearchCheck.class});
for(Failure i:aTest.getFailures())
{
System.out.println(i.getException()+"\nat: "+i.getDescription());
System.out.println("trace: "+i.getTrace());
System.out.println();
}
}
Thanks everyone
Edit: actually testSuite also works extremely, and is probably the better way to do this.
Related
Running all the java tests takes so much time to finish.
So the solutions is to run all the tests in parallel using multiple threads to run functions using #RunWith(ParallelSuite.class) in all classes.
But the problem is that there is some functions when they run in parallel they use the same resources. So it throws a ConcurrentModificationException. The solutions is to set this functions and to run them sequentially.
How to isolate some function to work sequentially after configuring all junit test in a spring boot project to work in parallel using ParallelSuite otherwise is there any other solution for doing that.
Use synchronized for functions that need to be accessed by one thread at a time or you can do within a single function using :
void runSync() {
synchronized (this) {
count = count + 1;
}
}
You probably also should read this.
Here is comment about setting default timezone for test code in #Before method of JUnit test. But TimeZone.setDefault is static method. Can it affect other test which are run after test with #Before and TimeZone.setDefault completes successful?
There are many things to check here, it depends on how do you run the tests.
The following factors may come into considerations:
Since you've tagged "maven" in a question: Maven's surefire/failsafe plugins responsible for running the tests can run multiple tests simultaneously in 1 or many JVMs, it all depends on their configurations.
So tests may start failing sporadically during the build even if they pass locally.
#Before and #After are called before and after each test in the test case respectively. #After is called even if the test fails. So probably memorizing the default timezone and setting it back after the test should be ok, but not "re-setting" the state in an "#After" block may lead to incorrect definitions in subsequent tests.
The better approach IMHO is using java.time.Clock abstraction. See this question for examples
Another possible option is refactoring a code to use some "factory" for providing current date / time. Then in Unit Test you could instantiate this factory and "inject" it as a dependency into the code-under-test. A kind of hand-crafted Clock
It will effect the other tests (as you assumed), as it won't be reset after running a single test.
Either reset it to "normal" by an #After method, or maybe change the code to take/inject the timestamp for "now" and make the code do it's calculation from there. From my experience this will give you alot more flexibility.
I want to test javaFX application by using junit. I checked several approaches as follow:
user TestFX api: these api works sometime on a test method, in other word, for some execution of a special test, it's work !!!
List use JFXRunner : I used test runner which is defined in this question, but it for a few of the methods to be run and when number of test methods be large(e.g. >20) , test methods waiting forever !!!
define a test runner class such as JavaFXThreadingRule which has been intruduced in this question. these approach works only for a single test and when run some test, test method go into waiting forever same as JFXRunner.
what is the problem really? by debugging test method, I found that by using lath for initializing javaFX, after running several method, remaining method will be waiting for ever. When I set a special time for awaiting method of lath, it work's correctly by it is not logically to set a constant time to test application. How can solve this problem.
for approach 2,3; I used Platform.exit() command for exiting from GUI Application; so test runner which use latch to initialize JavaFX platform wait forever. So, it was my code fault.So, calling Platform.exit() method prevents to execute remaining test methods.
I am noticing that JUnit (on Eclipse Indigo at least) is reporting the time of each test, including the #Before and #After setup and tear down functions.
I'd like to only see the time it takes the individual #Test functions to run, sans the overhead setup and teardown.
Don't get me wrong, I can see why including the setup and tear down can be important, but for my purposes, separating them would be more beneficial.
These tests really ought to take a matter of milliseconds. Hopefully I can do this without having to write my own/use Apache stopwatch function!
// Using JUnit 4, Java 6, Eclipse Indigo
Could there be a way to overload/override the JUnit reported time of test run with a manual #?
UPDATE:
It appears the timing of the function is in private methods found here:
https://github.com/junit-team/junit/blob/72af03c49fdad5f10e36c7eb4e7045feb971d253/src/main/java/org/junit/runner/Result.java
Since they aren't protected, I don't think we can override the start and stop times...
You can include a time stamp as the first line in your #Test functions, and another time stamp as the last line in the #Test functions. A simple calculation of the difference between the two times would show how long the test itself take without the setup and tear down parts.
I have 100 test methods. After each test, I need to perform some actions (data cleanup). Each of these 100 tests have different actions. These 100 test are not in one package or class. They are distributed.
How can I achieve this?
Right now, if a test passes, the cleanup happens, since it is part of the test. However, if the test fails, the cleanup doesn't happen. How can I make this work?
Any pointers would help.
If the tests do not have any common cleanup, you can ensure the test gets cleaned up from within the test method using a try/finally block, something like:
try {
// do test
}
finally {
// do cleanup
}
If there is any common cleanup between the test methods you could use #AfterMethod to do the cleaup.
In your case, it doesn't sound like there is much common cleanup, so the first may work better for you. It might also be worth considering if you need 100 different cleanup methods or if there can be any common setup/cleanup.
#AfterMethod would mean that you would need that every class gets this method. So you would need to go and edit each class/method. Same for #AfterGroups.
What I would suggest is to implement the IInvokedMethodListener. This would give you beforeInvocation and afterInvocation methods. In the afterInvocation method, implement your cleanup code.
Create a suite file with all of your tests which need this cleanup and specify this listener.
Hope it helps.
It sounds like you may already be using #AfterMethod to cleanup after the tests. To make #AfterMethod work after a failure, you need to use:
#AfterMethod(alwaysRun=true)
You can use groups and run a #AfterGroups somewhere. There's a #BeforeGroups as well. Setting it up with build tooling is a bit tedious and there are some interactions with IDEs as well. There's a BeforeSuite and AfterSuite as well, I believe.
An alternative could be using Spring and using the same spring context in all your tests (spring context gets reused that way). You can then do some things when the context is destroyed after your tests.