问题
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:
- 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.
- I created a file called create-snapshot and created the snapshot there.
- I created another file called restore-snapshot and restored the snapshot in that file.
- I added two entries in package.json's scripts like
"create-ss": "ts-node ./create-snapshot.ts"
,"restore-ss": "ts-node ./restore-snapshot.ts"
- 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