I have c# NUnit tests that are using async Tasks.
Is it OK for me to call the TeatDown method as an async as well?
So far it seems my test(s) are terminating BEFORE the whole async TearDown method completes itself.
Is it OK for me to call the TeatDown method as an async as well?
Yes. It is fine for any of the NUnit setup or teardown methods to be marked as async methods. There are many existing explanations on what marking a method as async does, but the only impact it should have on your tests is it will allow you to await other async methods.
For example:
[OneTimeSetUp]
public async Task OneTimeSetUp()
{
Console.WriteLine("OneTimeSetUp");
}
[SetUp]
public async Task SetUp()
{
Console.WriteLine("SetUp");
}
[Test]
public async Task Test1()
{
Console.WriteLine("Test1");
}
[Test]
public async Task Test2()
{
Console.WriteLine("Test2");
}
[TearDown]
public async Task TearDown()
{
Console.WriteLine("TearDown");
}
[OneTimeTearDown]
public async Task OneTimeTearDown()
{
Console.WriteLine("OneTimeTearDown");
}
Executing this test class will print the below output, showing the order in which these methods will execute in.
OneTimeSetUp
SetUp
Test1
TearDown
SetUp
Test2
TearDown
OneTimeTearDown
A couple of things that maybe you are already aware of that might be the cause of what you are seeing.
The difference between TearDown and OneTimeTearDown. TearDown executes after each test, OneTimeTearDown executes when the entire test fixture is complete. NLog Docs
The level of parallelization the tests are running. If you have multiple tests all running in parallel, TearDown methods will run after each test is complete. This could potentially be executing at the same time as your other tests if they are set to run in parallel. If you have any shared fields/properties etc being accessed in your tear down methods that are used across multiple tests it could cause issues.
Related
I am trying to test multiple features in one test.js file with each feature implemented as a test. All these tests can be run only after login to the portal. Testcafe closes the browser after the first test which fails the subsequent tests. I used Role and .beforeEach so that the tests can log in to the portal and proceed as usual but is there any easy way to just continue all the tests in the same browser session without really closing them after each test?
I used Role feature and .beforeEach which looks like a workaround to me. Is there any other way to run all tests one after another without really closing the browser session. This will save us the login operation and the instability that might cause for each test.
import { Selector , ClientFunction, Role } from 'testcafe';
import loginpage from '../../features/blah/login/page-model'
const appPortalUser2 = Role('https://test.com', async t => {
await loginpage.signInToPortal()
await loginpage.login('test-userm', 'Password123')
}, { preserveUrl: true });
fixture `portal - settings`
.page `https://test.com/apps`
.beforeEach (async t => {
await t`enter code here`
.useRole(appPortalUser2)
});
test('settings', async t => {
//test1
await loginpage.foo1()
});
test('backup', async t => {
//test2
await loginpage.foo2()
});
Actual behavior: after test1 browser exits and login page appears which fails test2 without using .beforeEach.
Expected: browser session should continue for test2 after test1 without .beforeEach. Provide such an option where tests can continue without creating new browser sessions each time.
At the moment, there is no such option in the public API.
The idea is that one test should not affect another test in any way. If all tests had run in one browser session, every test would have influenced all preceding tests as it could have had a page with an invalid state.
Struggling with acceptance tests. Started with basic login test:
import { test } from 'qunit';
import moduleForAcceptance from 'static/tests/helpers/module-for-acceptance';
moduleForAcceptance('Acceptance | authentication');
test('login', function(assert) {
visit('/');
click('.nav-bar__login-link');
andThen(function() {
assert.notOk(find('.login-form__submit-button').attr('disabled'));
});
fillIn('.login-form__email-block input', "ruz#email.com");
fillIn('.login-form__password-block input', "qwe");
click('.login-form__submit-button');
andThen(function() {
console.log("ftw");
assert.equal(find('.nav-bar__profile-link').text(), "some");
});
});
The problem is that andThen callback is called before authentication completes. It's jQuery ajax request and a few promises after. From what I can see ember waits for ajax query to complete, but doesn't wait for promises to get resolved/rejected. Should this test work out of the box? Do I have to write a custom waiter?
It sounds like your promises may not be setup right? But no, you should be able to write tests using the acceptance test helpers and not need to worry about async calls settling (or promises resolving) yourself
I am facing the problem that if I try to access the ContactManager from a BackgroundTask I get a System.IO.FileLoadException .
The same code works well when calling it from my main application:
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
test();
deferral.Complete();
}
private async void test()
{
ContactStore contactStore = await ContactManager.RequestStoreAsync();
}
The BackgroundTasks starts normally, but as soon as I call "test" the exception is thrown.
try to change your async void test to async Task test
Ok got it...
Reason was that I switched to VS2017 while developing and got a wrong dependency in the project.json of the backgroundtask:
While the main project had:
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
},
the background task got:
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
},
after editing to 5.0.0 all went perfect
Unfortunately I am now uninstalling VS2015 to install 2017 one so I can't test by my own but some of APIs are not available in Background Tasks so test the method I will mention :
1- Create a new Solution with type Class Library .
2- Put methods you want there .
3- Make the class library a reference for your background task solution .
3- Call your methods in this way classLib.className.FuncName();
4- See if it work or not.
I want to test my application behavior using Selenium. But I want the Yii::$app component behavior can be mocked from the test runner?
Let's say I have a class
Foo extends \yii\base\Component {
public function bar() {
//return something
}
}
And in the configuration I attach as Yii::$app component as Yii::$app->foo.
[
'foo' => [
'class' => 'Foo',
]
]
So the acceptance test works by running a web server and the test runner will run Selenium against the web server.
In the test runner I want to mock the Yii::$app->foo->bar() method so that it will return some string.
Usually when it comes from the database, we can mock by using Fixture. Test runner will fill the database using fixture that we set, and then the application component will read from that database.
How should I do this mock an arbitrary application component in the application run by the web server from the test runner?
Since start(), stop() will be removed in Qunit 2.0, what is the alternative for async setups and teardowns via the beforeEach, afterEach methods? For instance, if I want the beforeEach to wait for a promise to be finished?
QUnit basically wants people to stop using the global methods (not just start() and stop(), but also test(), expect(), etc). So, as of version 1.16.0, you should always use either the global namespace (QUnit) or the assert API argument passed into the test() functions. This includes the new async control:
QUnit.test( "testing async action", function( assert ) { // <-- note the `assert` argument here
var done = assert.async(); // tell QUnit we're doing async actions and
// hold onto the function it returns for later
setTimeout(function() { // do some async stuff
assert.ok( true, "This happened 100 ms later!" );
done(); // using the function returned from `assert.async()` we
// tell QUnit we're don with async actions
}, 100);
});
If you are familiar with the old start() and stop() way of doing things, you should see that this is extremely similar, but more compartmentalized and extensible.
Because the async() method call is on the assert argument into the test, it cannot be used in the beforeEach() function. If you have an example of how you were doing that before, please post it and we can try to figure out how to git it into the new way.
UPDATE
My mistake previously, the assert object is being passed into the beforeEach and afterEach callbacks on modules, so you should be able to do the same logic that you would do for a test:
QUnit.module('set of tests', {
beforeEach: function(assert) {
var done = assert.async();
doSomethingAsync(function() {
done(); // tell QUnit you're good to go.
});
}
});
(tested in QUnit 1.17.1)
Seeing that nobody has answered the beforeEach/afterEach part: a test suite is supposed to run as soon as the page loads. When that is not immediately possible, then resort to configuring QUnit:
QUnit.config.autostart = false;
and continue with setting up your test suite (initializing tests, feeding them to QUnit, asynchronously waiting for some components to load, be it AJAX or anything else), your site, and finally, when it's ready, just run:
QUnit.start();
QUnit's docsite has it covered.
Ember Qunit, has once exists beforeEach/setup, afterEach/teardown co-exist for a little while.
See PR: https://github.com/emberjs/ember-qunit/pull/125