{"id":16376704,"url":"https://github.com/wiktor-k/testresult","last_synced_at":"2025-04-03T03:13:27.830Z","repository":{"id":61288766,"uuid":"550231957","full_name":"wiktor-k/testresult","owner":"wiktor-k","description":"Provides TestResult type for concise and precise test failures","archived":false,"fork":false,"pushed_at":"2024-07-22T09:45:51.000Z","size":50,"stargazers_count":59,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-12T03:25:42.074Z","etag":null,"topics":["rust","testing"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/testresult","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wiktor-k.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"wiktor-k"}},"created_at":"2022-10-12T12:16:28.000Z","updated_at":"2024-09-17T10:11:28.000Z","dependencies_parsed_at":"2024-03-13T17:02:45.368Z","dependency_job_id":"55d4f503-3ba7-4c37-a1e0-c86dc1116feb","html_url":"https://github.com/wiktor-k/testresult","commit_stats":{"total_commits":10,"total_committers":2,"mean_commits":5.0,"dds":0.09999999999999998,"last_synced_commit":"506173df603f89f030b140e63318713118970239"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiktor-k%2Ftestresult","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiktor-k%2Ftestresult/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiktor-k%2Ftestresult/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiktor-k%2Ftestresult/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wiktor-k","download_url":"https://codeload.github.com/wiktor-k/testresult/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246927839,"owners_count":20856198,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rust","testing"],"created_at":"2024-10-11T03:25:48.512Z","updated_at":"2025-04-03T03:13:27.810Z","avatar_url":"https://github.com/wiktor-k.png","language":"Rust","funding_links":["https://github.com/sponsors/wiktor-k"],"categories":[],"sub_categories":[],"readme":"# Test result\n\n[![CI](https://github.com/wiktor-k/testresult/actions/workflows/rust.yml/badge.svg)](https://github.com/wiktor-k/testresult/actions/workflows/rust.yml)\n[![Crates.io](https://img.shields.io/crates/v/testresult)](https://crates.io/crates/testresult)\n[![Codecov](https://img.shields.io/codecov/c/gh/wiktor-k/testresult)](https://app.codecov.io/gh/wiktor-k/testresult)\n\n\nProvides `TestResult` type that can be used in tests to avoid\n`unwrap`s but at the same time to have precise stacktraces with the\npoint of failure clearly written.\n\nIt's like a lean [`anyhow`](https://crates.io/crates/anyhow) for tests!\n\n## Details\n\nConsider the following code. It uses `unwrap` so the test failure\nstacktrace will informative. Unfortunately it's not as concise as it\ncould be:\n\n```rust\n#[test]\nfn it_works() {\n   // ...\n   std::fs::File::open(\"this-file-does-not-exist\").unwrap();\n   // ...\n}\n```\n\nImproved version of this code uses `Result` and the `?` operator:\n\n```rust\n#[test]\nfn it_works() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n   // ...\n   std::fs::File::open(\"this-file-does-not-exist\")?;\n   // ...\n   Ok(())\n}\n```\n\nRunning the following code with `RUST_BACKTRACE=1 cargo test` shows\nthe following stacktrace:\n\n```text\n---- tests::it_works stdout ----\nthread 'tests::it_works' panicked at 'assertion failed: `(left == right)`\n  left: `1`,\n  ...\n   4: test::assert_test_result\n             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/test/src/lib.rs:184:5\n   5: testresult::tests::it_works::{{closure}}\n             at ./src/lib.rs:52:5\n   6: core::ops::function::FnOnce::call_once\n             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5\n  ...\nnote: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.\n```\n\nUnfortunately even though the test function location is recorded, the\nexact line where the test failure occurred is not present in the\nbacktrace.\n\nLet's adjust the test result type to use `TestResult`. This is the\nonly change compared to previous example:\n\n```rust\n#[test]\nfn it_works() -\u003e TestResult {\n   // ...\n   std::fs::File::open(\"this-file-does-not-exist\")?;\n   // ...\n   Ok(())\n}\n```\n\nRunning it again with `cargo test` shows more details:\n\n```text\n---- tests::it_works stdout ----\nthread 'tests::it_works' panicked at 'error: std::io::error::Error - No such file or directory (os error 2)', src/lib.rs:53:9\n```\n\nNote that the error location is now in the backtrace and also in the test failure message. This means that we don't\neven need the backtrace to know where the error happened.\n\nThe advantages of using `TestResult`:\n  - exact failure line is present in the test failure and the backtrace,\n  - the underlying error type and message are present in the test failure,\n  - the signature of the test result is simpler.\n\nFor a more elaborate description see [\"Improving failure messages in Rust tests returning a Result\"][IMPROV].\n\n[IMPROV]: https://bluxte.net/musings/2023/01/08/improving_failure_messages_rust_tests/\n\n## License\n\nThis project is licensed under either of:\n\n  - [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0),\n  - [MIT license](https://opensource.org/licenses/MIT).\n\nat your option.\n\n### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally\nsubmitted for inclusion in this crate by you, as defined in the\nApache-2.0 license, shall be dual licensed as above, without any\nadditional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwiktor-k%2Ftestresult","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwiktor-k%2Ftestresult","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwiktor-k%2Ftestresult/lists"}