验收测试
Acceptance testing is the process of taking an unmodified version of our application and testing it from the "outside" to make sure it behaves in a way we expect. Typically if an app passes acceptance tests, we have done our job properly from a product perspective.
As acceptance tests test the behavior of the application in a full browser context in a generic way, there are a range of tools that you can to specify and run such tests. In this guide we'll demonstrate using Chimp, an acceptance testing tool with a few neat Meteor-specific features that makes it easy to use.
Chimp requires node version 4 or 5. You can check your node version by running:
node -v
You can install version 4 from nodejs.org or version 5 with brew install node
. Then we can install the Chimp tool globally using:
npm install --global chimp
Note that you can also install Chimp as a
devDependency
in yourpackage.json
but you may run into problems deploying your application as it includes binary dependencies. You can avoid such problems by runningmeteor npm prune
to remove non-production dependencies before deploying.
Chimp has a variety of options for setting it up, but we can add some npm scripts which will run the currently tests we define in Chimp's two main modes. We can add them to our package.json
:
{
"scripts": {
"chimp-watch": "chimp --ddp=http://localhost:3000 --watch --mocha --path=tests",
"chimp-test": "chimp --mocha --path=tests"
}
}
Chimp will now look in the tests/
directory (otherwise ignored by the Meteor tool) for files in which you define acceptance tests. In the Todos example app, we define a simple test that ensures we can click the "create list" button.
/* eslint-env mocha */
/* eslint-disable func-names, prefer-arrow-callback */
// These are Chimp globals
/* globals browser assert server */
function countLists() {
browser.waitForExist('.list-todo');
const elements = browser.elements('.list-todo');
return elements.value.length;
};
describe('list ui', function () {
beforeEach(function () {
browser.url('http://localhost:3000');
server.call('generateFixtures');
});
it('can create a list @watch', function () {
const initialCount = countLists();
browser.click('.js-new-list');
assert.equal(countLists(), initialCount + 1);
});
});
运行验收测试
To run acceptance tests, we simply need to start our Meteor app as usual, and point Chimp at it.
In one terminal, we can do:
meteor
In another:
meteor npm run chimp-watch
The chimp-watch
command will then run the test in a browser, and continue to re-run it as we change the test or the application. (Note that the test assumes we are running the app on port 3000
).
Thus it's a good way to develop the test---this is why chimp has a feature where we mark tests with a @watch
in the name to call out the tests we want to work on (running our entire acceptance test suite can be time consuming in a large application).
The chimp-test
command will run all of the tests once only and is good for testing that our suite passes, either as a manual step, or as part of a continuous integration process.
创建数据
Although we can run the acceptance test against our "pure" Meteor app, as we've done above, it often makes sense to start our meteor server with a special test driver, tmeasday:acceptance-test-driver
. (You'll need to meteor add
it to your app):
meteor test --full-app --driver-package tmeasday:acceptance-test-driver
The advantage of running our acceptance test suite pointed at an app that runs in full app test mode is that all of the data generating methods that we've created remain available. Otherwise the acceptance-test-driver
does nothing.
In Chimp tests, you have a DDP connection to the server available on the server
variable. You can thus use server.call()
(which is wrapped to be synchronous in Chimp tests) to call these methods. This is a convenient way to share data preparation code between acceptance and integration tests.