-
Notifications
You must be signed in to change notification settings - Fork 87
Test Creation Guide
skyhit edited this page Feb 24, 2014
·
1 revision
- Open console in tc-api directory
- Run
npm start
- Run
npm test
- Open console in tc-api directory
- Run
npm start
- Run
./node_modules/.bin/mocha ./test/test.some_test.file
- Required filename for test is
test.[api name].js
(example: test.tops.js). - All tests from contest winner, should be put directly under test directory.
- All reviewers tests should be put under corresponding directory under test directory, like accuracy, failure, security directory.
- 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, likeaccuracy/sqls
,failure/sqls
, etc. - All other test related files should be put under
test_files
directory. Test files for accuracy, failure, security tests should be put underaccuracy/test_files
,failure/test_files
, etc.
-
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
-
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**
-
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**.
- 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
-
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
-
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
- var testHelper = require('./helpers/testHelper');
- var SQL_DIR = __dirname + "/sqls/xyz/";
- describe('Test xyz API', function () {
-
/**
-
* Clear database
-
* @param {Function<err>} done the callback
-
*/
-
function clearDb(done) {
-
testHelper.runSqlFile(SQL_DIR + " tcs_dw__clean", " tcs_dw ", cb);
-
}
-
/**
-
* This function is run before all tests.
-
* Generate tests data.
-
* @param {Function<err>} done the callback
-
*/
-
before(function (done) {
-
async.waterfall([
-
clearDb,
-
function (cb) {
-
testHelper.runSqlFile(SQL_DIR + "tcs_dw__insert_test_data", "tcs_dw", cb);
-
}
-
], done);
-
});
-
/**
-
* This function is run after all tests.
-
* Clean up all data.
-
* @param {Function<err>} done the callback
-
*/
-
after(function (done) {
-
clearDb(done);
-
});
-
//test cases go here
- });
For read only API is better to use before and after hooks instead of *beforeEach* and *afterEach*, because data generation is slow.
- 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
- before(function (done) {
-
async.waterfall([
-
clearDb,
-
function (cb) {
-
var files = testHelper.generatePartPaths(SQL_DIR + "topcoder_dw__insert_test_data", "", 2);
-
testHelper.runSqlFiles(files, "topcoder_dw", cb);
-
}
-
], done);
- });
`testHelper.generatePartPaths` method simply add suffix `.partX` to first parameter.
- Running json file
```javascript
- before(function (done) {
-
async.waterfall([
-
clearDb,
-
function (cb) {
-
testHelper.runSqlFromJSON(SQL_DIR + "common_oltp__insert_test_data.json", cb);
-
}
-
], done);
- });