Skip to content

Test Creation Guide

skyhit edited this page Feb 24, 2014 · 1 revision

1. Run all tests

  1. Open console in tc-api directory
  2. Run npm start
  3. Run npm test

2. Run single test

  1. Open console in tc-api directory
  2. Run npm start
  3. Run ./node_modules/.bin/mocha ./test/test.some_test.file

3. Name convection and directory structure

  1. Required filename for test is test.[api name].js (example: test.tops.js).
  2. All tests from contest winner, should be put directly under test directory.
  3. All reviewers tests should be put under corresponding directory under test directory, like accuracy, failure, security directory.
  4. All sql files used for tests presetup and teardown should be put under sqls directory. Sqls files for accuracy, failure, security tests, will be in different directory, directly under the accuracy, failure, security directory, like accuracy/sqls, failure/sqls, etc.
  5. All other test related files should be put under test_files directory. Test files for accuracy, failure, security tests should be put under accuracy/test_files, failure/test_files, etc.

4. Prepare scripts for test data and cleanup

  1. Scripts location Put sql scripts under <base_dir>/sqls/<api_name>/ directory. Submitter tests must be put under /test/ directory. Reviewer tests must be put under /test/accuracy/, /test/failure/ or /test/security/ directory.
*Example:*
> Submitter created 2 sql files for Tops API: common_oltp__clean,  common_oltp__insert_test_data.
> Above files should be put under /test/sqls/tops/ directory
  1. Naming convention Suggested file name has format: <database_name>__<type>_<optional_description>
*Examples:*
> * Script that cleans data in **common_oltp database**, may have name **common_oltp__clean**.
> * Script that inserts  sample data to **common_oltp** database, may have name **common_oltp__insert_test_data**.
> * Script that returns some data (SELECT query) from **common_oltp** database, may have name **common_oltp__select_user**
  1. Script size limit Maximum script size is 64KB. If a file is bigger, then Informix driver will throw exception. Suggested solution is to split file into parts and add suffix to file name .partX where X is part number.
*Examples:*
> **topcoder_dw__insert_test_data** is too big file. 
> We split it manually to **topcoder_dw__insert_test_data.part1** and **topcoder_dw__insert_test_data.part2**.
  1. JSON format (Recommended) Queries can be defined by .json package.
```json

{ "name" : "<script_name>", "db" : "<database_name>", "sqlfile" : "<sql_file_name>" } ```

Sqlfile can be single file or an array of files

5. Create Tests

  1. Hooks Mocha provides 4 hooks: * before() - executed once before all tests * after() - executed once after all tests * beforeEach() - executed before each test * afterEach() - executed after each test

  2. Test template Below is simple sample how to setup and cleanup data. Before all test we execute scripts /sqls/xyz/tcs_dw__clean and /sqls/xyz/tcs_dw__insert_test_data. After all test we execute only /sqls/xyz/tcs_dw__clean.

```javascript
  1. var testHelper = require('./helpers/testHelper');
  2. var SQL_DIR = __dirname + "/sqls/xyz/";
  3. describe('Test xyz API', function () {
  4.  /**
    
  5.   * Clear database
    
  6.   * @param {Function<err>} done the callback
    
  7.   */
    
  8.  function clearDb(done) {
    
  9.     testHelper.runSqlFile(SQL_DIR + " tcs_dw__clean", " tcs_dw ", cb);
    
  10. }
    
  11. /**
    
  12.  * This function is run before all tests.
    
  13.  * Generate tests data.
    
  14.  * @param {Function<err>} done the callback
    
  15.  */
    
  16. before(function (done) {
    
  17.     async.waterfall([
    
  18.         clearDb,
    
  19.         function (cb) {
    
  20.             testHelper.runSqlFile(SQL_DIR + "tcs_dw__insert_test_data", "tcs_dw", cb);
    
  21.         }
    
  22.     ], done);
    
  23. });
    
  24. /**
    
  25.  * This function is run after all tests.
    
  26.  * Clean up all data.
    
  27.  * @param {Function<err>} done the callback
    
  28.  */
    
  29. after(function (done) {
    
  30.     clearDb(done);
    
  31. });
    
  32. //test cases go here
    
  33. });
    
    For read only API is better to use before and after hooks instead of *beforeEach* and *afterEach*, because data generation is slow.
    
    
  34. Running partial scripts Maximum file size is 64KB , because of this we may have to run multiple partial scripts.
*Example:*
> We would like to run below scripts:
> * topcoder_dw__insert_test_data.part1
> * topcoder_dw__insert_test_data.part2

Sample before hook may look like:

```javascript
  1. before(function (done) {
  2.  async.waterfall([
    
  3.      clearDb,
    
  4.      function (cb) {
    
  5.          var files = testHelper.generatePartPaths(SQL_DIR + "topcoder_dw__insert_test_data", "", 2);
    
  6.          testHelper.runSqlFiles(files, "topcoder_dw", cb);
    
  7.      }
    
  8.  ], done);
    
  9. });
    
    `testHelper.generatePartPaths` method simply add suffix `.partX` to first parameter.
    
    
  10. Running json file
```javascript
  1. before(function (done) {
  2.  async.waterfall([
    
  3.      clearDb,
    
  4.      function (cb) {
    
  5.          testHelper.runSqlFromJSON(SQL_DIR + "common_oltp__insert_test_data.json", cb);
    
  6.      }
    
  7.  ], done);
    
  8. });