Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between describe() and it() in spec.js file in protractor?

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() {
    ..
  });
});
like image 960
Bhawani Singh Avatar asked Sep 12 '17 07:09

Bhawani Singh


People also ask

What is spec file in protractor?

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.

What is the first parameter in protractor?

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?

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.

What is the difference between spec file and describe-block?

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 .


2 Answers

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 Tests

A 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, like describe 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 and it blocks are functions, they can contain any executable code necessary to implement the test. JavaScript scoping rules apply, so variables declared in a describe are available to any it block inside the suite.

For more details, you can see this link

like image 93
Ghulam Mohayudin Avatar answered Oct 06 '22 00:10

Ghulam Mohayudin


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).

like image 27
FCin Avatar answered Oct 05 '22 23:10

FCin