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
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 } }
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.
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.
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.
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.
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 { ... } }
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With