Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using --format=json with --nocapture #54669

Closed
mchernyavsky opened this issue Sep 29, 2018 · 0 comments · Fixed by #62600
Closed

Using --format=json with --nocapture #54669

mchernyavsky opened this issue Sep 29, 2018 · 0 comments · Fixed by #62600
Labels
A-libtest Area: #[test] related T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue.

Comments

@mchernyavsky
Copy link

I'm looking for a way to get test results, including stdout/stderr, in json format.

I've tried to use --format=json with --nocapture and have got an unexpected result, imo.

Example file:

#[cfg(test)]
mod tests {
    #[test]
    fn test1() {
        println!("Hello from test #1");
        panic!();
    }
    #[test]
    fn test2() {
        println!("Hello from test #2");
    }
}
  1. cargo test -- -Z unstable-options --format=json output:
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s                                                                                                                        
     Running target/debug/deps/rust_sandbox-3aca71fd58cbc041
{ "type": "suite", "event": "started", "test_count": "0" }
{ "type": "suite", "event": "ok", "passed": 0, "failed": 0, "allowed_fail": 0, "ignored": 0, "measured": 0, "filtered_out": "0" }
     Running target/debug/deps/tests-03a860f74891dd25
{ "type": "suite", "event": "started", "test_count": "2" }
{ "type": "test", "event": "started", "name": "tests::test1" }
{ "type": "test", "event": "started", "name": "tests::test2" }
{ "type": "test", "name": "tests::test2", "event": "ok" }
{ "type": "test", "name": "tests::test1", "event": "failed", "stdout": "Hello from test #1\nthread 'tests::test1' panicked at 'explicit panic', tests/tests.rs:6:9\nnote: Run with `RUST_BACKTRACE=1` for a backtrace.\n" }
{ "type": "suite", "event": "failed", "passed": 1, "failed": 1, "allowed_fail": 0, "ignored": 0, "measured": 0, "filtered_out": "0" }
error: test failed, to rerun pass '--test tests'
  1. cargo test -- --nocapture -Z unstable-options --format=json output:
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s                                                                                                                        
     Running target/debug/deps/rust_sandbox-3aca71fd58cbc041
{ "type": "suite", "event": "started", "test_count": "0" }
{ "type": "suite", "event": "ok", "passed": 0, "failed": 0, "allowed_fail": 0, "ignored": 0, "measured": 0, "filtered_out": "0" }
     Running target/debug/deps/tests-03a860f74891dd25
{ "type": "suite", "event": "started", "test_count": "2" }
{ "type": "test", "event": "started", "name": "tests::test1" }
{ "type": "test", "event": "started", "name": "tests::test2" }
Hello from test #1
Hello from test #2
thread 'tests::test1' panicked at 'explicit panic', tests/tests.rs:6:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
{ "type": "test", "name": "tests::test2", "event": "ok" }
{ "type": "test", "name": "tests::test1", "event": "failed" }
{ "type": "suite", "event": "failed", "passed": 1, "failed": 1, "allowed_fail": 0, "ignored": 0, "measured": 0, "filtered_out": "0" }
error: test failed, to rerun pass '--test tests'

I expected to see this happen:

  • In the 1st case, the Hello from test #1 line doesn't appear in the stdout field.
  • In the 2nd case, the println! output and errors are recorded in different fields (e.g. stdout and stderr).

Expected behavior can be useful for integrating test frameworks with IDEs and editors.

I'm using:

  • rustc 1.29.1 (b801ae6 2018-09-20)
@zackmdavis zackmdavis added A-libtest Area: #[test] related T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue. labels Sep 29, 2018
bors bot added a commit to intellij-rust/intellij-rust that referenced this issue Oct 11, 2018
2901: RUN: Support test window r=vlad20012 a=mchernyavsky

Closes #1485.

This PR is mostly based on #2337 (a huge thanks to @farodin91 and @mkaput).

<img width="640" alt="screen shot 2018-10-09 at 22 13 58" src="https://user-images.githubusercontent.com/6079006/46692771-c61afa80-cc10-11e8-8281-e91a9309c3d3.png">

I had to remove the `--nocapture` option because of rust-lang/rust#54669.

TODO:
- [x] Provide execution time of each test
- [x] Provide location hint and proper line marker for test suites
- [ ] Add more tests

What else can be implemented/improved?
- [ ] Add more filters
- [ ] Support "Run only tests that failed/crashed after last run"

Co-authored-by: Jansen Jan <farodin91@sek-server.de>
Co-authored-by: Jan Jansen <farodin91@googlemail.com>
Co-authored-by: Mikhail Chernyavsky <chemike47@gmail.com>
bors bot added a commit to intellij-rust/intellij-rust that referenced this issue Oct 11, 2018
2901: RUN: Support test window r=vlad20012 a=mchernyavsky

Closes #1485.

This PR is mostly based on #2337 (a huge thanks to @farodin91 and @mkaput).

<img width="640" alt="screen shot 2018-10-09 at 22 13 58" src="https://user-images.githubusercontent.com/6079006/46692771-c61afa80-cc10-11e8-8281-e91a9309c3d3.png">

I had to remove the `--nocapture` option because of rust-lang/rust#54669.

TODO:
- [x] Provide execution time of each test
- [x] Provide location hint and proper line marker for test suites
- [ ] Add more tests

What else can be implemented/improved?
- [ ] Add more filters
- [ ] Support "Run only tests that failed/crashed after last run"

Co-authored-by: Jansen Jan <farodin91@sek-server.de>
Co-authored-by: Jan Jansen <farodin91@googlemail.com>
Co-authored-by: Mikhail Chernyavsky <chemike47@gmail.com>
bors bot added a commit to intellij-rust/intellij-rust that referenced this issue Oct 11, 2018
2901: RUN: Support test window r=vlad20012 a=mchernyavsky

Closes #1485.

This PR is mostly based on #2337 (a huge thanks to @farodin91 and @mkaput).

<img width="640" alt="screen shot 2018-10-09 at 22 13 58" src="https://user-images.githubusercontent.com/6079006/46692771-c61afa80-cc10-11e8-8281-e91a9309c3d3.png">

I had to remove the `--nocapture` option because of rust-lang/rust#54669.

TODO:
- [x] Provide execution time of each test
- [x] Provide location hint and proper line marker for test suites
- [ ] Add more tests

What else can be implemented/improved?
- [ ] Add more filters
- [ ] Support "Run only tests that failed/crashed after last run"

Co-authored-by: Jansen Jan <farodin91@sek-server.de>
Co-authored-by: Jan Jansen <farodin91@googlemail.com>
Co-authored-by: Mikhail Chernyavsky <chemike47@gmail.com>
bors bot added a commit to intellij-rust/intellij-rust that referenced this issue Nov 7, 2018
3004: RUN: Support `--nocapture` option r=vlad20012 a=mchernyavsky

Closes #2979.

Restores a checkbox in the `Edit Configurations` that has been removed in [this PR](#2901).

If this checkbox is selected, the `--nocapture` option will be used in the tests. The checkbox not selected by default.

At the moment, the test window can't work with `--nocapture` (see rust-lang/rust#54669), so when the checkbox is activated, the test results will be displayed in the old-style console.

<img width="500" alt="Edit Configuration Menu" src="https://user-images.githubusercontent.com/6079006/47786381-add46200-dd1c-11e8-946a-6c4e6f1cdf1f.png">

Co-authored-by: Mikhail Chernyavsky <chemike47@gmail.com>
Centril added a commit to Centril/rust that referenced this issue Aug 27, 2019
…=gnzlbg

libtest: add --show-output flag to print stdout of successful tests

This pull request adds a new flag `--show-output` for tests to show the output of successful tests. For most formatters this was already supported just not exposed via the CLI (apparently only used by `librustdoc`). I've also added support for this option in the JSON formatter.

This kind of fixes rust-lang#54669 which wants `--format json` to work with `--nocapture`, which is... well, impossible. What this issue really calls for is `--show-output` as implemented here.
@bors bors closed this as completed in 6d20265 Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-libtest Area: #[test] related T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants