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

Need way to run some code before any of unit tests #35762

Closed
Dushistov opened this Issue Aug 17, 2016 · 5 comments

Comments

Projects
None yet
6 participants
@Dushistov
Copy link
Contributor

Dushistov commented Aug 17, 2016

The details can be found logging while running cargo test.

The problem is in usage of rust log library,
to get it work you need call "initialization" code that provide implementation of log::Log trait
to logging macros.

So to get output from your code that tested by "cargo test" you need somehow call "logging_init" function, but as can I see at now there is no way to tell "cargo" run something before any tests.

Previously reported as cargo bug

@Stebalien

This comment has been minimized.

Copy link
Contributor

Stebalien commented Aug 17, 2016

You can call let _ = env_logger::init(); (it's safe to call this function multiple times, only the first call will count) at the top of each test but this is far from perfect.

@durka

This comment has been minimized.

Copy link
Contributor

durka commented Aug 18, 2016

Another workaround is to put the initialization code in a lazy static, and make sure to access said static at the beginning of every test (a macro can help). Then it will really only run once.

@Stebalien

This comment has been minimized.

Copy link
Contributor

Stebalien commented Aug 18, 2016

@durka If you're going to do that, you might as well just use sync::Once.

@adamhjk

This comment has been minimized.

Copy link
Contributor

adamhjk commented Feb 1, 2017

A similar, and even more difficult issue, is if you want to run teardown after all the tests have run.

@dtolnay

This comment has been minimized.

Copy link
Member

dtolnay commented Nov 15, 2017

I agree that setup and teardown for tests would be useful. This is going to need an RFC so let's track it under rust-lang/rfcs#1664.

@dtolnay dtolnay closed this Nov 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment