Testcafe - How to run code after all the fixtures are run

天涯浪子 提交于 2021-02-10 13:21:21

问题


I want to create a snapshot of the SQL server DB and then restore it after "all the fixtures" are run.

I can do it after every fixture through .after hook in a fixture. However, that is causing issues while running tests since the DB may be still in transition after a restore. So I would prefer to do it after all the fixtures.


回答1:


I have found a workaround for now. The workaround is:

  1. Add dependency for ts-node. I also had to add tsconfig.json with compiler options' lib property set to es2015 and dom. If not, it was complaining about Promise.
  2. I created a file called create-snapshot and created the snapshot there.
  3. I created another file called restore-snapshot and restored the snapshot in that file.
  4. I added two entries in package.json's scripts like "create-ss": "ts-node ./create-snapshot.ts", "restore-ss": "ts-node ./restore-snapshot.ts"
  5. Now from Powershell, I run the tests with the command: npm run create-ss;npm run test-chrome-hl;npm run restore-ss. This runs the commands sequentially in Powershell. In other terminals you may need to use && or something else instead of ;.

I can avoid "npm run create-ss" by using the .before hook of the fixture by keeping track of a variable to ensure it runs only once. However I cannot do a similar approach when the last test gets executed.

Its a bit of a pain to remember the three commands but I dont see another way so far.




回答2:


You can also use the TestCafe Programming Interface API. The TestCafe Runner class returns a Promise object. You can use this object to run your custom cleanup code after all tests/fixtures are completed.

Here's an example:  

const createTestCafe = require('testcafe');
let testcafe         = null;

createTestCafe('localhost', 1337, 1338)
    .then(tc => {
        testcafe     = tc;
        const runner = testcafe.createRunner();

        return runner
            .src(['tests/fixture1.js', 'tests/fixture2.js', 'tests/fixture3.js'])
            .browsers(['chrome', 'safari'])
            .run();
    })
    .then(failedCount => {
        // Clean up your database here...
        testcafe.close();
    });


来源:https://stackoverflow.com/questions/53270589/testcafe-how-to-run-code-after-all-the-fixtures-are-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!