When writing the test case in spec.js file in protractor then 2 fields are showing describe() and it(). what exactly the use of them and when to use?
// spec.js
describe('Protractor Demo App', function() {
it('should have a title', function() {
..
});
});
What is Spec File in Protractor? Spec File contains the specs or automated test cases commonly termed as a Test Scripts File. Protractor tests are written using the syntax of the test framework. In this tutorial, we will be using Jasmine Test Framework. It is simple and easy to understand and most widely used.
The first parameter is a string which can be the name of your Collection of Specs or a Feature. For example, if you are writing a test script for login functionality, the name of the describe block can be Login Feature or Login Suite Protractor Basics - What is It-Block in Protractor?
What is Configuration File in Protractor? Configuration file as the name suggests is the file that will have all the configuration in order to run the Spec File ( Feature file or Collection of TestCases ). There can be multiple things which can be mentioned in the configuration file like: Note: We will cover all the details in subsequent articles.
As mentioned above, Spec file in protractor is kind of a feature file in BDD, Describe-block is a feature in the feature file. Describe is a function in jasmine it takes two parameters namely a string and a function. The first parameter is a string which can be the name of your Collection of Specs or a Feature .
By reading this hopefully you will come to know your answer.
spec.js
about (how Jasmine a behavior-driven development framework for testing JavaScript code works)
It has two main functions
Suite
describe
Your TestsA test suite begins with a call to the global Jasmine function describe with two parameters: a string and a function. The string is a name or title for a spec suite - usually what is being tested. The function is a block of code that implements the suite.
Specs
Specs are defined by calling the global Jasmine function
it
, which, likedescribe
takes a string and a function. The string is the title of the spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code. An expectation in Jasmine is an assertion that is either true or false. A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec.describe("A suite", function() { it("contains spec with an expectation", function() { expect(true).toBe(true); }); });
It's Just Functions
Since
describe
andit
blocks are functions, they can contain any executable code necessary to implement the test. JavaScript scoping rules apply, so variables declared in adescribe
are available to anyit
block inside the suite.
For more details, you can see this link
it
is an actual test with logic. describe
is a container for tests which allows you to divide tests into multiple parts. describe
blocks can wrap tests that act on the same part of an application or have something in common, for example:
describe('Menu tests', function() {
it('should open menu', function() {
});
it('should select menu', function() {
});
it('should close menu', function() {
});
});
describe
exposes a couple of functions, such us:
beforeAll
- runs before first test.beforeEach
- runs before each test.And many more.
it
cannot or at least shouldn't contain describe
or it
blocks inside itself, whereas describe
is supposed to contain it
blocks as well as helper blocks (e.g. beforeAll
).
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