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

Is there a way to test private functions? #1

Open
IoaNNUwU opened this issue Apr 22, 2024 · 3 comments
Open

Is there a way to test private functions? #1

IoaNNUwU opened this issue Apr 22, 2024 · 3 comments

Comments

@IoaNNUwU
Copy link

Hi, thanks for your great work! This is my favorite benchmarking library so far.

But I wonder, is there a way to test private functions? For example, I have a crate which has 1 public function as it's API, but on this function call it determines which internal implementation to use. I would like to bench those internal implementations without making them public.

@sarah-quinones
Copy link
Owner

it's a problem i commonly run into as well, but i don't know of a solution for it

@mert-kurttutan
Copy link

I use the method of using feature-dependent reexporting. I found it from the following link:
https://www.reddit.com/r/rust/comments/9rnuhi/what_should_i_use_today_for_benchmarking_in_rust/
Then, you can activate feature when calling your bench command.

@IoaNNUwU
Copy link
Author

Hi, I did some research on this issue.

I didn't like this feature-dependant reexporting because it seems like too much pain just to make benchmarks. After investigation, turns out you can test private functions right now without any problem. You just have to put them under #[test] in your private module and use Bench::new(BenchConfig::default()) to not depend on command line arguments. I made example repo here: https://github.com/IoaNNUwU/diol-private-example .

Main problem is your benchmarks are run when you do cargo test which is not usually what you want. We can solve this issue by using either custom #[cfg(bench)] options or #[feature(bench)], but you have to use cargo test --features bench to run them. But even then benches are run with tests, which is not ideal, but acceptable in my opinion.

I think further research in needed. This is indeed how I would write my benchmarks instead of only testing public APIs, but problems are:

  • Is there a good way to integrate private tests with cargo bench command
  • Is there a way to run specific group of tests instead of using feature-flags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants