What is the idiomatic way to test for a private function?

The book in Rust says that using the β€œtests” module is an idiomatic way of conducting unit tests. But I do not see the function from the supermodule in the test module if this function is not marked "pub". How should internal functions be checked?

My first instinct was to look for a way to #ifdef pub keyword. I have done this in the past for testing in C ++. For Rust, I did just tests for private functions in the module, and then tests for the open interface in the "tests" module.

Am I doing it right?

+4
source share
2 answers

Configure the test module inside the module containing private methods or structures:

 mod inners { fn my_func() -> u8 { 42 } mod test { #[test] fn is_answer() { assert_eq!(42, super::my_func()); } } } 

Of course, I do not agree that you should check personal things in general, but this is another discussion.

+5
source

An idiomatic way of checking a particular function is not. Unit tests should test the social behavior of a class. Private methods are just implementation details of the above public methods that you should check.

+3
source

All Articles