Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel execution of tests

Tags:

scala

sbt

specs2

I've noticed that SBT is running my specs2 tests in parallel. This seems good, except one of my tests involves reading and writing from a file and hence fails unpredictably, e.g. see below.

Are there any better options than

  1. setting all tests to run in serial,
  2. using separate file names and tear-downs for each test?
class WriteAndReadSpec extends Specification{   val file = new File("testFiles/tmp.txt")    "WriteAndRead" should {     "work once" in {       new FileWriter(file, false).append("Foo").close       Source.fromFile(file).getLines().toList(0) must_== "Foo"     }     "work twice" in {       new FileWriter(file, false).append("Bar").close       Source.fromFile(file).getLines().toList(0) must_== "Bar"     }   }    trait TearDown extends After {     def after = if(file.exists) file.delete   } } 
like image 931
Pengin Avatar asked Nov 06 '11 12:11

Pengin


People also ask

What does it mean to run tests in parallel?

Parallel Testing is a process to leverage automation testing capabilities by allowing the execution of the same tests simultaneously in multiple environments, real device combinations, and browser configurations. The overarching goal of parallel testing is to reduce time and resource constraints.

What supports parallel test execution?

One such remarkable feature is the seamless support for Parallel Testing. The TestNG test automation framework allows you to run tests in parallel or multithreaded mode by utilizing the Multi-Threading concept of Java.

How many tests will run at a time in parallel execution?

What is Parallel Testing? Parallel testing means running multiple automated tests simultaneously to shorten the overall start-to-end runtime of a test suite. For example, if 10 tests take a total of 10 minutes to run, then 2 parallel processes could execute 5 tests each and cut the total runtime down to 5 minutes.

How do we do parallel execution of a scenario in TestNG?

TestNG provides multiple ways to execute tests in separate threads. In testng. xml, if we set 'parallel' attribute on the tag to 'tests', testNG will run all the '@Test' methods in tag in the same thread, but each tag will be in a separate thread. This helps us to run test methods / classes / tests in parallel.


2 Answers

In addition to that is written about sbt above, you must know that specs2 runs all the examples of your specifications concurrently by default.

You can still declare that, for a given specification, the examples must be executed sequentially. To do that, you simply add sequential to the beginning of your specification:

class WriteAndReadSpec extends Specification{   val file = new File("testFiles/tmp.txt")    sequential    "WriteAndRead" should {    ...   } } 
like image 60
Eric Avatar answered Sep 20 '22 10:09

Eric


Fixed sequence of tests for suites can lead to interdependency of test cases and burden in maintenance.

I would prefer to test without touching the file system (no matter either it is business logic or serialization code), or if it is inevitable (as for testing integration with file feeds) then would use creating temporary files:

// Create temp file. File temp = File.createTempFile("pattern", ".suffix"); // Delete temp file when program exits. temp.deleteOnExit(); 
like image 45
Andriy Plokhotnyuk Avatar answered Sep 21 '22 10:09

Andriy Plokhotnyuk