NUnits: Start automation tests parallel in a defined order (Selenium) - nunit

Im writing automation tests for our website using NUnit and Selenium.
We have 2 different users (aminInt and hsuInt) and 3 features which need to be tested (in the example below TestA, TestB, TestC)
In my example below, there are in total 6 automation tests in the test explorer as each feature is being tested with both users.
Everything works. Each test is getting its own webDriver and all the tests are independent.
Now i want to start the tests in parallel.
I already tried everything i could find online. Tried the different parameter in parallelizable, but i cant get it right.
I would like to start 2 test at a time.
For example:
First test:
TestA adminInt
TestA hsuInt
after both tests above are done, it should start:
Second Test:
TestB adminInt
TestB hsuInt

If your goal is to save time and your tests are truly independent, then trying to control the order of execution isn't what you want. Essentially, it's NUnit's job to figure out how to run in parallel. Your job is merely to tell NUnit whether the tests you wrote are capable of running in parallel.
To tell NUnit which tests may be run in parallel, use the [Non]ParallelizableAttribute. If you place attributes on the fixtures, their different meaning is as follows...
[NonParallelizable] means that the fixture is not capable of running in parallel with any other fixtures. That's the default if you don't specify any attribute.
[Parallelizable] means that the fixture is capable of running in parallel with other fixtures in your test.
[Parallelizable(ParallelScope.All)] means that both the fixture and all the individual tests under that fixture are capable of running in parallel.
[Parallelizable(ParallelScope.Children] means that the fixture is not capable of running in parallel with other fixtures but the test methods under it may run in parallel with one another.
I stressed capable above because that's what you should focus on. Don't use the attribute with the expectation that NUnit will run some tests together with other specific tests because there is no NUnit feature to do that. Keep your focus on what you can control... writing independent, parallelizable tests and telling NUnit which ones they are.
If you find that NUnit starts too many test threads at one time, you can place a LevelOfParallelism attribute in your AssemblyInfo.cs. However, keep in mind that NUnit defaults depending on the number of cores available. So what works for you on your development machine may not give the best performance in your CI builds. My advice is to let NUnit use its defaults for most things until you see a reason to override them.

Thanks for the answers.
I found a solution for how to start just 2 tests in parallel.
With this parameter in front of the class, only 2 tests will start in parallel.
//[assembly: LevelOfParallelism(2)] You need this parameter only inside of one class. So if you have different classes which have their own tests, add this parameter to only one class and it will run all tests in parallel as long as you have the fixture command too.
[TestFixture("userA")]
[TestFixture("userB")]
To start the test for 2 different users for example.

Related

Parallel execution on specflow xunit

I am looking to maximize the parallel execution of Specflow + Xunit, currently the parallel is up to the feature level. I am looking for a way to make it up to scenario level. I do plan to use specflow+runner, unfortunately it has stopped development.
Currently I did the workaround using DevOps pipeline to distribute the scenarios to 2 agents. Does someone have any better suggestions?
Calculator.feature
Feature: Calculator
Scenario: Add two numbers
...
#agent2 // Assign to agent 2
Scenario: Add two numbers 2
...
Calculator2.feature
Feature: Calculator2
Scenario: Add two numbers 3
...
#agent2 // Assign to agent 2
Scenario: Add two numbers 4
...
This appears to be a limitation of xUnit. From Running Tests in Parallel on xunit.net:
Test Collections
How does xUnit.net decide which tests can run against each other in parallel? It uses a concept called test collections to make that decision.
By default, each test class is a unique test collection. Tests within the same test class will not run in parallel against each other.
(emphasis, mine)
A feature file in SpecFlow gets parsed and converted to a test class in xUnit. A test class (and therefore a feature file in SpecFlow) become default collections to support parallelization when running tests. Until xUnit supports a way to run test methods in the same class in parallel, this does not appear possible when using xUnit + SpecFlow, because it is not possible when just using xUnit.

Are Dart/Flutter tests executed synchronously or asynchronously

could someone explain please how Flutter/Dart tests are executed using test runner?
Are the tests executed synchronously or asynchronously?
Does the testing framework execute every single test synchronously, meaning that only a single test and test suite is executed at any single time?
Or does the testing framework only execute a single test at a time within a test suite, but are able to execute multiple test suites at the same time?
Or testing framework run all tests and test suites completely independent of each other at the same time, completely asynchronously?
This is important because it has a direct impact on the way we are or aren't able to structure our tests, especially when it comes to the set up and tear downs of tests, and the way we assert functionality is working correctly.
Thanks!
In general, dart test will execute many tests in parallel (the parallelism level varies based on CPU core count), but you can disable this with a command line flag.
You should not write tests with any inter-dependence (i.e. one test should not rely on some global state set up by another test). For example, you may find that because your laptop has a different CPU configuration to your CI server, your tests might pass locally but fail in CI due to different ordering.
If you have some setup logic that is very expensive, and needs to be reused between multiple tests, you can use setUpAll() to run some code once before every test in a test group, however this is still discouraged. Personally, I prefer to just join the tests into one long test, to keep all tests self-contained.
This has some advantages. For example, you can use --total-shards and --shard-index to parallelize tests in CI (by creating multiple jobs to each run a different subset of the test suite).
You can also randomize the order of your tests with --test-randomize-ordering-seed, to make sure you aren't accidentally setting up such dependences between tests that might invalidate their results (i.e. perhaps test2 only passes if it happens after test1, randomizing the ordering will catch this).
TLDR
Many tests run in parallel. Try to keep tests self-contained, with no dependence on the order of tests. Extract setup logic into functions and pass it into setUp. If you really really need the performance, you can try setUpAll but avoid it if possible.

Using NUnit for functional tests and guarantees about execution sequence and threads

I asked this auestion on NUnit-Discuss, but i realize that group is not very active, so i give it a try here:
We've been using MSTests until now for some functional tests.
I know, neither MSTest nor NUnit is really for functional test, but we need those tests with a simple integration in Visual Studio.
The tests will launch other executables, connect, do stuff, disconnect and kill the processes.
We're having trouble with MSTest in that it launched tests in a separate thread and seems that some execution is overlapping between tests, even when executed sequentially.
So i'm thinking about moving to NUnit.
The question i have is:
Can NUnit be configured in any way such as to give the following guarantees:
Tests will be executed sequentially, in an order that can be specified.
Tests will be executed from the same thread.
TearDown code of one test will have been fully executed before Setup code of a following test will be called.
If so, what would be that configuration, if any particular?
Thank you.
By default, NUnit does not execute any tests in parallel. If you never use the ParallelizableAttribute, then your tests run one at a time.
Of course, that does not mean your tests can't break NUnit, for example, by starting a thread or process that never terminates after the test thread terminates. NUnit only takes responsibility for the tests it runs itself.
NUnit does not guarantee that all tests will be executed from the same thread. That is a separate matter from parallelization, of course. Separate threads may be started for each thread, based on attributes you specify. You may, for example, designate some tests to run in a Single-threaded Apartment, while others run by default in an MTA. You might use the RequiresThreadAttribute, which asks NUnit to use a new thread for the test it decorates. You might use the SingleThreadedAttribute on a class, to indicate that all the code in that class runs on the same thread.
One trick, which is currently available but which may not exist in all future releases, is to specify --workers=0 on the command-line to nunit3-console. That tells NUnit to simply run the tests without creating any test workers and gives an execution path that more closely resembles that of NUnit V2.
So, in general, I think your needs can be met, but it could require some tinkering with your tests to make it work the way you want.

Run NUnit Test After All Others Are Complete

I have a situation (detailed below) in which I want to run one NUnit test after all the other tests have completed. I know that I can use the order attribute to start my tests in a certain order but in this case:
I want to attribute (or otherwise change) only one test out of several hundred.
I want this test to run last, not first.
I want this test to run after all other tests have completed, not after they've started.
I have experimented with OneTimeTearDown, but ideally this would run as a regular, named test and appear that way in the test results.
(Why)
I have several hundred named, hand-crafted tests that run against different folders of json test files. Non-programmers add files to these folders from time to time. The purpose of this final test is to introspect those folders and compare the contents on disk with the files for which a test has already been executed (these are recorded by each test). If this indicates that there are untested files that, itself, constitutes a test failure.
It's an interesting question. Basically you want a meta-test... one that tests how well you are testing. For that reason, it's logical for it to actually be a test.
Unfortunately, NUnit only supports this sort of "run after everything" in a OneTimeTearDown. Now, you can Perform assertions in a OneTimeTearDown, but any failures are treated as errors, i.e. unexpected exceptions. For some purposes, this may be workable, but it doesn't look quite the same as a failure.
If I were doing this, I think I'd make it a separate analytical step in my script, after the tests had been run.

Getting the current Experiment instance at runtime

I'm running JUnit 4 with AnyLogic. In one of my tests, I need access to the Experiment running the test. Is there any clean way to access it at runtime? E.g., is there a static method along the lines of Experiment.getRunningExperiment()?
There isn't a static method that I know of (and, if there was, it might be complicated by multi-run experiments which permit parallel execution, although perhaps not since there's still a single Experiment, though there'd be thread-safety issues).
However, you can use getEngine().getExperiment() from within a model. You probably need to explain more about your usage context. If you're using AnyLogic Pro and exporting the model to run standalone, then you should have access to the experiment instance anyway (as in the help "Running the model from outside without UI").
Are you trying to run JUnit tests from within an Experiment? If so, what's your general design? Obviously JUnit doesn't sit as well in that scenario since it 'expects' to be instantiating and running the thing to be tested. For my automated tests (where I can't export it standalone because I don't use AnyLogic Pro), I judged that it was easier to avoid JUnit (it's just a framework after all) and implement the tests 'directly' (by having my model components write outputs and, at the end of the run in the Experiment, having the Experiment compare the outputs to pre-prepared expected ones and flag if the test was passed or failed). With AnyLogic Pro, you could still export standalone and use JUnit to run the 'already-a-test' Experiments (with the JUnit test checking the Experiment for a testPassed Boolean being set at the end or whatever).
The fact that you want to get running experiments suggests that you're potentially doing this whilst runs are potentially executing. If so, could you explain a bit about your requirements?