I have sources in src/ directory and tests together with them in the same files. Then, in src/lib.rs I have this:
#[cfg(test)]
use simple_logger::SimpleLogger;
#[cfg(test)]
use log::LevelFilter;
#[cfg(test)]
#[ctor::ctor]
fn init() {
SimpleLogger::new()
.with_level(LevelFilter::Trace)
.init()
.unwrap();
}
This turns logging on for all unit tests.
Then, I have integration tests in tests/ directory. If I put the same code into tests/lib.rs, it makes no effect. Where can I put it so that logging is configured by integration tests the same way as it's done for unit tests?
The reason that this is not working is that integration tests are separate crates which depend on the lib crate - but without the #[cfg(test)] enabled. I'm not sure there even is a cfg flag you could use to know that the lib crate is being compiled for integration testing, but it would not be clean to do such a thing anyway. Instead, you could adopt the method described in Rust By Example: put the code you posted into a tests/common/mod.rs and add mod common; to all your integration tests. If that is still too much work per test, you could have your integration tests under a separate Cargo.toml of the same workspace, and have that Cargo.toml have a library that compiles the code you posted unconditionally (and never publish that to crates.io).
Related
I want to code a crate for a REST API. cargo test needs a server where the features can be tested, so I need something like adding "parameters" to the tests.
I looked at the documentation of cargo test to see if I can add command line options like cargo test -- --api-server=https://localhost:80 but it doesn't seem to exist.
I really love cargo and how easy it is to write unit tests.
However, it seems like it's testing functionality is fairly basic. What I'd like to be able to do is have named groups of tests somehow. What I am trying to accomplish is to have a default set of tests that execute when you run the basic cargo test. However, some of my tests take much longer to run, so I'd like to be able to move these to another group of extended tests that I can run with some command like cargo test --extended, and also the ability to be able to run all the tests at once easily. I also have a third group of tests that I have currently implemented as ignored tests so I can run them separately.
Even though all my tests are effectively unit tests, I tried to accomplish this by creating a tests directory as you would do with integration tests. However it seems that the basic cargo test command wants to run the all these tests, i.e. the normal tests that are part of my crate as well as the extended tests in the tests crate.
Does anyone know how to accomplish this or whether there is some crate that provides this functionality?
You could use a combination of feature flags and the #ignore macro as mentioned here: https://www.reddit.com/r/rust/comments/3i1nki/how_to_skip_expensive_tests_with_cargo_test/
My boss asked me to delete Non usable files in my project and asked me to delete ExampleUnitTest and ExampleInstrumentedTest What are these files used for and will it gonna be a problem if I deleted them?
A simple response is : No, there is no problem if you delete this files.
You can evaluate your app's logic using local unit tests when you need to run tests more quickly and don't need the fidelity and confidence associated with running tests on a real device. With this approach, you normally fulfill your dependency relationships using either Robolectric or a mocking framework, such as Mockito. Usually, the types of dependencies associated with your tests determine which tool you use:
If you have dependencies on the Android framework, particularly those that create complex interactions with the framework, it's better to include framework dependencies using Robolectric.
If your tests have minimal dependencies on the Android framework, or if the tests depend only on your own objects, it's fine to include mock dependencies using a mocking framework like Mockito.
Then instrumented unit tests are tests that run on physical devices and emulators. Instrumented tests provide more fidelity than local unit tests, but they run much more slowly. Therefore, we recommend using instrumented unit tests only in cases where you must test against the behavior of a real device.
Could anyone, please, recommend simple test coverage tool for Node.js without using Mocha because I use simple functions from assert module of Node in my own test framework.
The standard library for code coverage is nyc, so that's my suggestion; however, you aren't going to get code coverage to work with a test framework you came up with yourself for free -- you'll need to make sure you are instrumenting the appropriate source files so the report contains what you expect.
The following links, which discuss non-standard uses of nyc (non-standard meaning, not jasmine or mocha) might be useful - one for classic istanbul, one for the newer nyc:
https://github.com/gotwarlost/istanbul/issues/574
https://github.com/istanbuljs/nyc/issues/548
You can use Jest and Supertest.
Jest -> https://jestjs.io/
SuperTest -> https://www.npmjs.com/package/supertest
I want to write a custom build.rs script that generates some diagrams to accompany the documentation for a crate I'm working on. I want this script to run only when I run cargo doc, not the other profiles (cargo build, cargo test, ...). What would be the best way to do that?
I was hoping that cargo would pass this info to build.rs in the PROFILE env variable, but that seems to only contain "debug" or "release".
This is not possible as of Rust 1.47. Cargo issue #4001 tracks the possibility of supporting this in some fashion.