咨询Protractor/Mocha上一次测试运行结果的存储文件位置
Hey there! Since you're new to using Protractor with Mocha and Chai, let's clear this up for you: by default, Mocha doesn't automatically save your test run results to a flat file—you'll need to configure a reporter to do that. But the good news is it's pretty straightforward, and once set up, you'll have a persistent record of every test run (including test names, pass/fail statuses, and even error details).
How to Generate a Flat File of Test Results
Mocha has built-in reporters that can output results to a file in formats like JSON or XUnit (both are easy-to-parse flat files). Here are two ways to set this up:
1. Command Line Quick Setup
When you run your Protractor tests, add flags to specify the reporter and output file directly:
protractor your-conf-file.js --reporter json --reporter-options output=last-test-run.json
This will generate a last-test-run.json file in your project root. Open it up, and you'll find all the details you need: each test's name, whether it passed/failed, duration, and error messages if any.
2. Permanent Configuration in Protractor Config
To avoid typing those flags every time, add the reporter setup to your Protractor configuration file (usually conf.js):
exports.config = { // Your existing config (seleniumAddress, specs, etc.) seleniumAddress: 'http://localhost:4444/wd/hub', specs: ['test/**/*.spec.js'], mochaOpts: { // Use the JSON reporter (or 'xunit' if you prefer XML format) reporter: 'json', reporterOptions: { // Choose where to save the file—update the path to your preference output: './test-reports/last-run-results.json' } } };
Now every time you run protractor conf.js, the results will automatically save to the specified file. You can even change the reporter to xunit if you prefer an XML flat file instead of JSON.
What If You Already Ran Tests Without This Setup?
Unfortunately, if you didn't configure a reporter before running your tests, there's no built-in way to retrieve those lost results. Mocha only outputs to the console by default, so unless you saved the console output, those details are gone. But going forward, the setup above will ensure you never lose test results again.
Parsing the Flat File
Since the output is JSON (or XML for XUnit), you can easily parse it with tools like:
- A simple Node.js script to extract specific details (e.g., list all failed tests)
- Any JSON/XML parsing utility or library in your language of choice
- Even just opening the file in a text editor to scan through results manually
内容的提问来源于stack exchange,提问作者Eric27710

