UNPKG

3.18 kBMarkdownView Raw
1### Best Practices for writing tests
2
3##### Test Prefix
4Make sure to use a testPrefix while creating a new suite (test/framework.cli-test.js). This testPrefix would be the directory in which the test recordings would be stored.
5
6##### Effectively using suite.generateId()
7- Recording ids for playback
8 - For the generated random Ids to be saved in the recording files, suite.generateId() should be called **inside** "setupSuite()", "setupTest()" or inside your actual test "it()"
9- Deleting created artifacts
10 - Declare arrays in the test file to store ids created for different types of artifacts. These arrays are passed as second argument to the suite.generateId() method. During teardownTest(), teardownSuite() or at the end of your test, one can iterate over the arrays of respective items and delete the created items. This ensures leaving the environment in a clean state
11 ```javascript
12 var createdSites = [];
13 var sitePrefix = "test-site";
14 . . .
15 describe( . . .
16 before(function (done) {
17 suite = new CLITest(testprefix, requiredEnvironment);
18 suite.setupSuite(function () {
19 sitename = suite.generateId(sitePrefix, createdSites);
20 suite.execute("site create --location %s %s --json", process.env.AZURE_SITE_TEST_LOCATION,
21 sitename, function (result) {
22 result.exitStatus.should.equal(0);
23 done();
24 });
25 });
26 });
27 . . .
28 after(function (done) {
29 suite.teardownSuite(function () {
30 //delete all the artifacts that were created during setup
31 createdSites.forEach(function (item) {
32 suite.execute('site delete %s -q --json', item, function (result) {
33 result.exitStatus.should.equal(0);
34 });
35 });
36 done();
37 });
38 });
39 . . .
40```
41
42###### Ways to reduce playback time
43
44It is usually not important to playback the creation/deletion of Artifacts during setupSuite(), setupTest(), teardownSuite(), teardownTest(). These can be easily skipped during playback as follows, thus reducing the playback time:
45```javascript
46. . .
47 before(function (done) {
48 suite = new CLITest(testprefix, requiredEnvironment);
49 suite.setupSuite(function () {
50 sitename = suite.generateId(sitePrefix, createdSites);
51 //the following code will not be run during playback mode
52 if (!suite.isPlayback()) {
53 suite.execute("site create --location %s %s --json", process.env.AZURE_SITE_TEST_LOCATION,
54 sitename, function (result) {
55 result.exitStatus.should.equal(0);
56 done();
57 });
58 } else {
59 done();
60 }
61 });
62 });
63. . .
64```
65
66##### Effectively using suite.isMocked v/s suite.isRecording v/s suite.isPlayback()
67
68- suite.isMocked : This property has a boolean value. If **true**, it means that the suite is **NOT** running in **LIVE** mode. It could either be **PLAYBACK** or **RECORD** mode.
69- suite.isRecording : This property has a boolean value. If **true**, it means that the suite is running in **RECORD** mode only.
70- suite.isPlayback() : This function returns a boolean value. If **true**, it means that the suite is running **PLAYBACK** mode only.