mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 04:54:17 -06:00
39 lines
2.1 KiB
Markdown
39 lines
2.1 KiB
Markdown
# Writing Automated Tests
|
|
|
|
"Program testing can be a vary effective way to show the present of bugs, but it is hopelessly inadequate for showing their absence."
|
|
|
|
This doesnt mean that we shouldn't try to test as much as we can
|
|
|
|
Correctness in our programs is the extent to which our code does what it is intended it to do.
|
|
|
|
Rust is desgned with a high dgree of concern about the correctness of programs, correctness is complex and not easy to prove.
|
|
|
|
A large part of this compelxity is due to Rust's type system, but the type system cannot catach everything.
|
|
|
|
With that being said Rust includes support for writing automated software tests
|
|
|
|
For example lets say we wrote a functions `add_two` that adds 2 to whatever number is passed to it.
|
|
|
|
This function's signature accepts an integers as a parameter and returns an integer as a result.
|
|
|
|
When this is implemented and compiled, Rust will do all of the type checking and borrow checking to enusre that the code is good.
|
|
|
|
For instance we cannot pass a `String` value or an invalid reference to this function.
|
|
|
|
One thing Rust *can't* check is that this function will do precisely what we inteded it to doe, which is return the parameter plus 2 rather than the paramter plus 10 or minus 50
|
|
|
|
This is where tests come in
|
|
|
|
We can write tests that assrt, for example in the case where we pass `3` to the `add_two` function, the returned value is `5`.
|
|
|
|
We can run these tests whenever we make changes to our code to make sure any existing correct behavior has not changed.
|
|
|
|
This chapter will cover:
|
|
- [How to Write Tests](Writing_Tests.md)
|
|
- [Controlling How Tests Are Run](Test%20Controls.md)
|
|
- [Test Organization](Test_Organization.md)
|
|
|
|
But it will also talk about Rust's testing facilities, the annotations and macros available to you when writing tests, the default behavior and options provided for running your tests and how to organize tests into unit tests and integration tests.
|
|
|
|
Test are still important to check that the functionality, logic or expectations to behave work even with all of Rusts checks (type system and ownership rules) to prevnt bugs.
|