Created
July 10, 2017 14:37
-
-
Save joelwatson/84ba5903dd7f286a9b55a6551c60afa5 to your computer and use it in GitHub Desktop.
Simple example of a data-driven test using fast-csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Make | Model | Price | |
---|---|---|---|
Ford | Escort | 12000 | |
Chevy | Malibu | 14000 | |
Dodge | Ram | 20000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in WebDriver scenario, init npm project | |
npm init | |
// now install the fast-csv module, and save it | |
npm install fast-csv --save |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("data-driven test", function() { | |
let rows; | |
// we'll create an asynchronous beforeAll() method to read in our data | |
beforeAll(function (done) { | |
rows = []; | |
// require needed modules | |
let fs = require('fs'); | |
let csv = require('fast-csv'); | |
// read in the csv file contents | |
let stream = fs.createReadStream('/my/path/to/data.csv'); | |
// use fast-csv; this particular file has headers, so let it know about it | |
let csvStream = csv({headers : true}) | |
.on('data', function (data) { | |
// for each row, push the data into our rows[] object | |
// NOTE: this module also provides validation if certain rows need exclusion, | |
// as well as transformation callbacks if the data needs to be massaged after it's read | |
rows.push(data); | |
}) | |
.on('end', function () { | |
// done reading the file; we can call done() to tell Sencha Test to continue with specs | |
done(); | |
}); | |
stream.pipe(csvStream); | |
}); | |
afterAll(function () { | |
rows = null; | |
}); | |
it('should use external data', function () { | |
// rows[] should now be populated with the correct data, so we can iterate over it if desired to execute | |
// whatever expectations we need | |
rows.forEach(function (data) { | |
... | |
}) | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment