What is the difference between describe() and it() in spec.js file in protractor?
Solution 1
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
Solution 2
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
).
Solution 3
It's BDD interface (Behavior-driven development).
describe
is used to describe an action. A top level describe can be used to describe the entire suite with a single word that can be grepped.
describe("getUser", function() {
...
});
it
block simply contains assertions on objects defined up the closure scope.
it("should have status code 200", function() {
assert.equal(res.statusCode, 200, "statusCode is not 200")
});
Solution 4
In my understanding, describe
is an description of your funcionality and it
are yours steps and expects for ther funcionality.
Here exemplify better: https://docs.angularjs.org/guide/e2e-testing
Bhawani Singh
I am a Test Automation Engineer. I am having the experience to work on selenium, cucumber, AutoIt, Sikuli and many more testing tools and frameworks. Recently I have started working on Protracter Automation tool. I am a keen observer and quick learner. I love challenges and always march forwarding to learn new technologies and frameworks.
Updated on June 21, 2022Comments
-
Bhawani Singh almost 2 years
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() { .. }); });