#Run code only for unit tests

30 messages · Page 1 of 1 (latest)

cloud quarry
#

How do I execute a line of code only when it is for unit tests? Say I have the following function:
fn foo() {
let run_this = true;
If run_this {
connect_to_internet();
}
}

When foo() gets ran for a unit test, I want run_this to be set to false. I tried doing something like:

Let mut run_this = true;
#[cfg(test)] {
run_this = false;
}

But it’s not working as expected

icy coral
#

also use

let mut run_this = true;
// <...>

```rust
let mut run_this = true;
// <...>
```

#

bc god its hard to read

cloud quarry
cloud quarry
icy coral
#

why do you need to do this?

cloud quarry
#
fn foo() {
  let run_this = true;
  if run_this {
    connect_to_internet();
  }
}

#[cfg(test)]
mod tests {
#[test]
  fn uut() {
    foo();
  }
}
icy coral
#

why not just directly:

#[cfg(test)]
mod tests {
#[test]
  fn uut() {
    connect_to_internet();
  }
}
cloud quarry
# icy coral why do you need to do this?

to be more specific, this is the function I am testing

fn create_tls_connector(&self) -> io::Result<TlsConnector> {
    let mut builder = native_tls::TlsConnector::builder();
    if !self.certificate_path.is_empty() {
        debug!("opening cert: {}", self.certificate_path);
        let mut file = File::open(&self.certificate_path)?;
        let mut cert_pem = vec![];
        file.read_to_end(&mut cert_pem)?;
        builder
            .add_root_certificate(
                Certificate::from_pem(&cert_pem).expect("couldn't read pem")
            );

        if cfg!(test) {
            debug!("allowing invalid certs for unit tests only");
            builder.danger_accept_invalid_certs(true);
        }
    }
    let cx = builder
        .build()
        .expect("couldn't build tlsconnector");

    Ok(tokio_native_tls::TlsConnector::from(cx))
}

I only want to run builder.danger_accept_invalid_certs(true); for unit tests

icy coral
#

also pls highlight ur code

icy coral
#

hmm

#
fn create_tls_connector(&self) -> io::Result<TlsConnector> {
    let mut builder = native_tls::TlsConnector::builder();
    if !self.certificate_path.is_empty() {
        debug!("opening cert: {}", self.certificate_path);
        let mut file = File::open(&self.certificate_path)?;
        let mut cert_pem = vec![];
        file.read_to_end(&mut cert_pem)?;
        builder
            .add_root_certificate(
                Certificate::from_pem(&cert_pem).expect("couldn't read pem")
            );

        if cfg!(test) {
            debug!("allowing invalid certs for unit tests only");
            builder.danger_accept_invalid_certs(true);
        }
    }
    let cx = builder
        .build()
        .expect("couldn't build tlsconnector");

    Ok(tokio_native_tls::TlsConnector::from(cx))
}
#

much better

cloud quarry
#

This is function of a bigger workflow

icy coral
#

okie

cloud quarry
#

I have integration tests that will connect to a server I create using self-signed certs

#

I'm just wondering why cfg!(test) is false when I run cargo test

icy coral
#

one question

#

why specifically do builder.danger_accept_invalid_certs(true); when you will be using this on tests only

#

and not on actual code?

#

tests are like supposed to test scenarios that will be seen in the app or library code

#

from other users interacting with it

#

running something only for tests is kind of useless

cloud quarry
#

fair point. Could be me needing to rethink how I write my unit tests

icy coral
#

ye

#

its like being a car manufacture, testing your car out with components that won't be availaible to the outer public