Question

I am running a series of tests with testng and am running them at the parallel="classes" level. The problem is I want 3-4 classes to always be ran together on the same thread (single threaded). All other classes can be ran on whatever thread, I do not care. Is there a way to do this?

Example: A B and C need to be ran one-at-a-time. All other classes will be denoted by numbers. 
 Threads running at one time:    
    A 3 7 2 - A would run while 3 other classes run
    C 8 4 1 - B would run once A is done, (other threads dont matter)
    B 5 9 6 - C would run once B is done.

For more information, I have 3-4 classes that can not be ran in parallel because of what they do, I basically just want these to be ran one at a time, and want to guarantee only one of these classes is ever running at once. All my other classes can be run at any given time in any order with no problems.

I know I can set parallel to the group level, but then I have to go add random pointless groups to all my other classes so that they run in the meantime. Perhaps there is a way to single thread one group and parallel another?

I currently am running 4 threads and have about 50 classes.

Any ideas?

-- Added requirements/thoughts based on comments --

The main reason I am trying to do it this way is I am testing a server API. Some of the calls take a long time on the server side, so while I am waiting for a response, I might as well send some other calls as well, hence the multi threading. I converted all my tests to run in multithreading in order to shave a very significant time off of the overall test suite. It works very well, however there are these four classes which can not be thrown at the server at the same time or this will cause test failures. Note: I am aware that one way would be to "fix these classes so that they can work in parallel" and I have tried that, and will continue to if possible, but that’s a different longer story, lets just now assume that is not possible.

Hence, its practically all about the time. Solutions which would in essence run single threaded by sleeping all other threads I am not interested in as that would defeat the whole purpose.

Was it helpful?

Solution 2

After many painstaking attempts of trying to find a workaround for this, I was reading the TestNG spec more clearly and found the solution. Its actually quite simple. The <suite> has a parallel level and so does the <test> That brought me to the following

<suite name="testSuite" parallel="tests" thread-count="4">
   <test name="foo" parallel="classes" thread-count="3">
     .. (classes you want to include)
   </test>
   <test name="bar">  <!-- this runs single threaded because there is no parallel tag>
     .. (classes you want to run single threaded)
   </test>
</suite>

OTHER TIPS

You can bind them all to the same cpu. This would mean they cannot be running at the time time.

The problem is that just having one at a timer running is rarely enough. Instead you should use locking between these threads so that they can hand over execution at known points and in a way you can reason about.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top