I have a set of mocha test scripts (= files), located in /test together with mocha.opts. It seems mocha is running all test files in parallel, is this correct? This could be a problem if test data are used in different test scripts.
How can I ensure, that each file is executed separately?
It seems mocha is running all test files in parallel, is this correct?
No.
By default, Mocha loads the test files sequentially, and records all tests that must be run, then it runs the test one by one, again sequentially. Mocha will not run two tests at the same time, no matter whether the tests are in the same file or in different files. Note that whether your tests are asynchronous or synchronous makes no difference: when Mocha starts an asynchronous test, it waits for it to complete before moving on to the next test.
There are tools that patch Mocha to run tests in parallel. So you may see demonstrations showing Mocha tests running in parallel, but this requires additional tools, and is not part of Mocha properly speaking.
If you are seeing behavior that suggest tests running in parallel, that's a bug in your code, or perhaps you are misinterpreting the results you are getting. Regarding bugs, it is possible to make mistakes and write code that will indicate to Mocha that your test is over, when in fact there are still asynchronous operations running. However, this is a bug in the test code, not a feature whereby Mocha is running tests in parallel.
Be careful when assigning environment variables outside of mocha hooks since the assignments to that variables are done in all files before any test execution (i.e eny "before*" or "it" hook).
Hence the value assigned to the environment variable in the first file will be overwritten in the second one, before any Mocha test hook execution.
Eg. if you are assigning process.env.PORT=5000 in test1.js file and process.env.PORT=6000 in test2.js outside of any mocha hook, then when the tests from test1.js starts execution the value of the process.env.PORT will be 6000 and not 5000 as you may expect.
Related
I have test suite where in one of the spec the 5th test-case is dependent on 3rd test-case. While the case is run locally via cypress runner - I do not see any issue in order of running.
But while case is running in CI - I'm seeing 5th is failing randomly [verified that no script errors] & upon analysis I notice that certain data records which are created in 3rd case are not returned for 5th case & hence its failing.
Is there a way to order tests within a spec in Cypress?
Unfortunately it is not possible to run tests within the same spec file in specific order in Cypress currently.
Cypress is basically just scheduling events to happen, with no additional control beyond that, so there is no way to guarantee tests will run in a specific order.
Sometimes, for whatever reason, one of the tests in cypress will get stuck and continue to run indefinitely until I manually stop the test suite. It completely ignores any timeouts already set up in cypress.
What is the best way to safeguard and automatically fail just that one test -- without halting the entire test suite?
Parallel execution of cucumber 4 work for me, but I want to execute some actions just once for all tests, It is a possible way to run some hooks in another Thread ?
As per your requirement, you want to execute some actions for all test cases once, is it like before or after all test case execution. If so then adding #BeforeClass from JUnit/TestNG and Similarly you can use #AfterClass in your run cuke class. This piece of code would run once before running your first class and after all test execution completed.
would it may work or adding tagged hooks give you some clue. Like for some specific test cases you can use tagged hooks and run those specific actions inside that hook only.
I have two tests directories. Unit tests, and integration tests. Both use mocha.
Unit tests run on average between 1-5 ms. Unfortunately our integration tests take longer. Some of them up to 30 seconds.
I was wondering if I could set the timeout to 30 seconds only for the test/integration directory, but leave test/unit using the default mocha timeout (2 seconds) in the mocha.opts file. Or perhaps have multiple mocha.opts files.
There's no support for multiple mocha.opts files being active for a single invocation of Mocha. You could have two Mocha invocations each with their own mocha.opts, however.
If you want everything in a single Mocha invocation, and set different timeouts for different parts of the suite, there's no direct way for telling Mocha "files in this directory have one timeout, and files in that other directory have another timeout". You are limited to calling this.timeout in your callbacks, like this:
describe("User view", function () {
this.timeout(...);
// Tests....
});
If you structure your suite so that all integration tests are seen by Mocha as being descendants of a single top describe, you can effectively set this timeout in only one location (the top describe) for all your integration tests. See this question and its answers for ways to structure a suite in this way.
Tools: Protractor 3.3.0, Jasmine 2.4.1, Selenium Standalone Server.
I have a test suite that has a plethora of spec.js files each containing unique tests for my application.
I'm using the maxInstances and shardTestFiles browser capabilities to launch 3 browsers and run each spec file to decrease the run time of the entire suite (with the 3 browsers its at about 20 minutes.. so without it's probably pushing over an hour).
My question is how can I tell Protractor to have a spec file wait for the completion of another spec file before executing. For example:
Lets say I have a Page 1 Tests and I have spec files a1.spec, a2.spec, and a3.spec and then I have some other tests of similar structure or what have you.
When I launch protractor with 3 browser instances, as expected, a1.spec, a2.spec, and a3.spec all launch with their own individual browser instance since it's a 1:1 ratio. BUT what if a3.spec can't run unless a2.spec completes? How do I make this wait occur, or is it just a best practice to not make certain tests dependent on each other?
You could use the done callback either in the beforeEach or it of any test which needs time to complete asynchronously. Any following tests won't run until the one you're waiting on has finished.
beforeEach Example
beforeEach(function(done) {
setTimeout(function() {
value = 0;
done();
}, 1);
});
it Example
it("should support async execution of test preparation and expectations", function(done) {
value++;
expect(value).toBeGreaterThan(0);
done();
});
This would queue as you need but the tests would be run serially. To speed this up you could use Promises and when/guard but this may complicate things too much.