Testing 凌驾于柏树之上';前后法

Testing 凌驾于柏树之上';前后法,testing,automation,cypress,Testing,Automation,Cypress,我们正在使用Cypress.io构建自动化套件。我们需要在每次测试之前为数据库添加种子,并在测试之后清除数据。这可以像下面这样做 describe('Our test suite', function() { before(function () { //loadDbSeed is a custom command that will load the seed file based on the spec file seed = cy.loadDbSeed() cy

我们正在使用Cypress.io构建自动化套件。我们需要在每次测试之前为数据库添加种子,并在测试之后清除数据。这可以像下面这样做

describe('Our test suite', function() {
  before(function () {
    //loadDbSeed is a custom command that will load the seed file based on the spec file
    seed = cy.loadDbSeed()
    cy.task('seed:db', seed)
  })

  it('should be true', function() {
    //Some test with some action followed by an assertion
    cy.visit('/some-page')
    cy.get('[data-cy="identifier"]')
      .click()
    expect(true).to.equal(true)
  })

  after(function () {
    // clearDb is a custom command that will clear out the DB.
    // We are still debating if we must clear the DB after the tests.
    // But we might still need to do some common actions for the entire suite after
    cy.clearDb()
  })
})
我们看到的问题是,所有测试套件都需要相同的
之前
之后
操作。所以我们想要覆盖这些方法,这样我们的测试就是这样的

describe('Our test suite', function() {
  before(function () {
    // DB seeding is done automatically
    // Write only custom before steps required for this test
  })

  it('should be true', function() {
    //Some test with some action followed by an assertion
    cy.visit('/some-page')
    cy.get('[data-cy="identifier"]')
      .click()
    expect(true).to.equal(true)
  })

  after(function () {
    // DB clearing is done automatically
    // Write only custom after steps required for this test
  })
})

我们如何做到这一点?我一直在搜索Cypress代码,没有发现任何明显的问题。

如果您查看Cypress文档,建议不要在之后使用
。我警告不要在全局范围内设置数据,您真的需要在每次测试中使用它吗?如果您需要在某个时间点以每次测试为基础输入数据,这会与此全局数据冲突吗?您可以在每次测试的基础上执行以下操作:

describe('Our test suite', function() {
  beforeEach(function () {
     cy.then(async () => {
       await MyDatabaseService.resetdata()
       await MyDatabaseService.createSomeData()
     });
   });

  it('should be true', function() {
    //Some test with some action followed by an assertion
  })
})
当特定的测试需要特定的数据时,我还必须按如下方式嵌套一些测试(如果这里的某些格式有点过时,很抱歉,希望它会有意义!)

对于上面的第二个测试,测试将运行所有三个数据库操作

结果是,如果您在运行测试之前清除了数据,那么它会使事情变得更清楚


我希望这有帮助。我自己是Cypress的新手,很难改掉我们在Selenium使用了一段时间的(坏?)习惯

您只需将
before()
after()
方法添加到“/cypress/support/index.js”中,它们就可以在每个套件中运行。请注意,在套件内部,您仍然可以有特定于套件的
before()
after()
,事实上,您可以有任意次数-它们按出现的顺序被调用(首先支持)。Richard,非常感谢。我们将尝试一下,看看它是否有效!西蒙,非常感谢你的意见。我没有在我的问题中说清楚(对此我深表歉意),但我们实际上并没有在全球范围内传播这些数据。每个规范或测试都有自己的seed-JSON文件,我们只需要一种机制在before块中加载相关的seed文件,而不必再次重写相同的代码。这有意义吗?关于after的使用,正如我在文章中提到的,我们仍在讨论这个问题。我不清楚的一个方面是,如果每个测试都没有清理数据库,我们如何确保测试彼此独立?例如,测试1在数据库中创建一个新项,并验证创建是否有效。测试2是一个独立的测试,通过搜索和筛选验证列表中的项目数。如果我们不重置和加载仅与测试2相关的数据,我们如何确保测试1不会影响测试2?我猜在这种情况下,Cypress建议测试1只删除它添加的项,而不是使用一个公共的after块重置整个数据库。是这样吗?
describe('Our test suite', function() {
  beforeEach(function () {
     cy.then(async () => {
       await MyDatabaseService.resetdata()
       await MyDatabaseService.createSomeData()
     });
   });

  it('should be true', function() {
    //Some test with some action followed by an assertion
  });

   describe('These tests need more data, this is a nested describe', function () {
     before(function () {
       cy.then(async () => {
         await MyDatabaseService.addSomeMoreData()
       });
     it('this test uses the extra data', function () {
     // Do some funky tests here
    });
   });
 });

})