如何在测试说明中使用在beforeEach中初始化的变量?

时间:2019-08-22 13:04:40

标签: jasmine protractor

我循环运行几个测试,我想在测试说明中使用在beforeEach中的变量

for(let i =0; i<3; i++){
        describe("action", () => {
          let variableToUse: string;
          beforeEach(async () => {
            variableToUse = await someAction(i);
          });
            it("some test desc " + variableToUse, async () => {
             //some test using variableToUse
            });
        });
}

我希望控制台显示“一些测试des varaibleToUseValue”,但实际输出是“一些未定义的测试desc”

4 个答案:

答案 0 :(得分:1)

如果我是你,我会这样做

for(let i =0; i<3; i++){
    let variableToUse: string;
    describe("action", () => {
        beforeEach(async () => {
            variableToUse = await someAction(i);
        });
        it("some test desc " + variableToUse, async () => {
            //some test using variableToUse
        });
    });
}

在这种情况下,您可以避免意外重用先前执行的值舞弊,因为它总是在循环内定义

答案 1 :(得分:0)

let variableToUse: string;
for(let i =0; i<3; i++){
        describe("action", () => {
          beforeEach(async () => {
            variableToUse = await someAction(i);
          });
            it("some test desc " + variableToUse, async () => {
             //some test using variableToUse
            });
        });
}

答案 2 :(得分:0)

这应该有效:

describe('testbeforeeach - ', function () {
  var TestSuiteName = this.getFullName();
  var TestVariable = null;    
  beforeEach(function () {
   TestVariable = "abc";
    return TestVariable;
  });    
  it(TestSuiteName + 'Test1', function () {        
      console.log("TestVariable for Test1 ====", TestVariable);        
  });    
  it(TestSuiteName + 'Test2', function () {       
      console.log("TestVariable for Test2 ====", TestVariable);
  });
});

答案 3 :(得分:0)

尝试下面给出的代码段

let variableToUse: number;

let i: number;

for(i =0; i<3; i++){
    describe("action", async () => {

      beforeEach(async () => {
         variableToUse =  i;
      });
        it("some test desc " + variableToUse, async () => {
         //some test using variableToUse
        });
    });
}