如何防止文件中的before和beforeEach应用于所有文件中的所有测试?

时间:2017-01-18 13:27:30

标签: javascript mocha

我使用mocha自动化了我的api。 结构就像 -

1.对于每个api,有一个 api_namex.js 文件,其中有mocha测试所在的文件

  1. 每个api_namex.js文件在 describe() describe()和多个 it() >

  2. 现在如果我在其中一个 api_namex.js 文件(比如api_name1.js)中使用 beforeEach(),那么同时运行整个测试套件涉及运行所有api_namex.js文件,调用所有这些文件的beforeEach()

  3. 我应该如何只为预期的api_namex.js文件运行? 与之前的问题相同。

    my_areas.js 文件附在下面。我在这里使用了beforeEach()。但是在运行整个测试套件时,这个beforeEach在每个测试用例之前被调用。我想要它在此describe()中的每个测试用例之前

    var should = require('should'),
        supertest = require('supertest'),
        servicesGenerator = require('../../utils/services_generator_test.js'),
        responseMsg = require('../../utils/response_messages.js'),
        helper = require('../../utils/helper.js'),
        testData = require('../../utils/test_data.js'),
        apiEndPoints = require('../../utils/api_endpoints.js');
    
    beforeEach(function (done) {
        clearMyAreas();
        done();
    });
    
    describe('My Areas', function () {
    
        it('1: All Data Valid', function (done) {
            servicesGenerator.postPlayoApi(apiEndPoints.myAreas)
                .send(getValidMyAreasBody())
                .end(function (err, res) {
                    baseValidator(err, res, 1, responseMsg.myAreasSuccess);
                    areasValidator(err, res, 1);
                    done();
                });
        });
    
        it('2: Invalid userId', function (done) {
            servicesGenerator.postPlayoApi(apiEndPoints.myAreas)
                .send(getValidMyAreasBody(testData.userIdInvalid))
                .end(function (err, res) {
                    baseValidator(err, res, 0, responseMsg.invalidUserId);
                    done();
                });
        });
       });
    

1 个答案:

答案 0 :(得分:3)

查看文档,您应该能够在beforeEach块中放置beforedescribe个钩子,它们应该在describe的上下文中运行:{{ 3}}

相关问题