As we know previously, we have metadata that we can add at the fixture
level or the test
level.
Here also we can filter tests and the fixtures by metadata.
You can run tests whose metadata contains specific values by using the --test-meta
argument to do this.
For example:
testcafe chrome ./my-tests/ --test-meta device=mobile,env=production
And to filter fixtures with specific data, use the --fixture-meta
argument.
For example:
testcafe chrome ./my-tests/ --fixture-meta device=mobile,env=production
We are adding --fixture-meta
and adding the values for the metadata that we are using.
In this demo, we will learn how to execute our test using filtering by metadata.
To run our tests with filtering by metadata, we can use the following command.
testcafe chrome tests/testMetaScript.js --test-meta env=production
So we can use testcafe
and add the browser chrome
, and then we can specify the file name testMetaScript.js
. Then we have --test-meta
and we can pass meta and add the env
, which is equal to production
.
Then we can run our test.
Here we have another test - "Second Test" - without any metadata.
So we just need to run this one with the metadata - "First Test" - or we need to filter our test with the metadata and execute this one only.
So here, our test only is running because this is our "First Test" and we are adding this metadata.
For example, if we add the wrong metadata - for example, here env
will be staging
. We don't have staging. We have the only production as metadata in this test.
What will happen during this test?
Let's run our test and check the result.
So TestCafe opens the browser, and here we have an error.
That the specific filter settings exclude all the tests because we don't have anything related to a staging environment metadata.
One additional feature from TestCafe that we can use is the -t
or --test
command line argument, or in the runner.filter
method - if we have a runner class - to run a test by name.
For example:
testcafe safari ./tests/samplefixture.js -t "Click a label"
From the command line in this example, we are running TestCafe on Safari, and this is a file with our test case.
And then we are pressing -t
, and then we will add the name of the test that we usually add in the string.
So this means that we will run a specific test file or a specific test method only.
Also, this is additional information - that if we have a runner class, we can add the browser name, we can add the source of the file that we need to run.
And here also, we can specify .filter
and add as a test name the value as a string.
And then we can add .run()
.
Here is an example:
await runner
.browsers('safari')
.src('./tests/samplefixture.js')
.filter(testName => testName === 'Click a label')
.run();
So one additional feature from TestCafe is that we can filter our tests or fixtures with only -t
.
In this demo, we will learn how to execute our test cases by filtering our tests or fixtures by the name.
To execute our test or to run our test by filtering with the name, we can do the following gesture.
For example, here, we have three test cases in HooksTest.js
.
We have the second test, the first test, and the third test.
So for example here, if we say:
testcafe chrome tests/HooksTest.js -t "First Test"
So here that means that I need just to run the first test from this feature file or this Javascript file.
We will run the test and check what happens during our test.
Enter the name. And after that select the option, or the operating system, and click submit.
Here we will find that yes, we only ran the first test.
So for running with a filter, you can just add -t
and add the name of the test that we added in our test case.
Quiz
The quiz for this chapter can be found in 5.4