Rust Testing and TDD - An Intro to Testing and Test Driven Development

preview_player
Показать описание
#Rust #TensorProgramming #OwnershipAndBorrowing

In this video we take a beginners look at Tests in Rust. We also touch on generic Boxing Errors and creating a Custom Result and we look at test driven development.

Good Programming Laptops:

Support the Channel and Join Patreon:

Dontate:
ETH: 0x03247265dd5242605bD2FA3c40fb3b70d9e3D685
Cardano: addr1q9auccwrr9ws8qdyv45f4qwsx76pfmld4zapks89sakq94ay0xmle73y0r8ruwd0zslls4eglf98lghru7ywv56cedysk7ftjt

Рекомендации по теме
Комментарии
Автор

Just a couple of things I would note - typically, when doing TDD, you would put each test into its own function. You would also want test for error results - for example, what might happen if someone added "apples" into the file you're reading from? That's something that would have its own test.

For those new to the idea of TDD, the points of it is that you write out the expected behaviour before you write out the logic that brings the behaviour about. As such, you write just enough of a test to fail, and then write the minimum possible code to make the test pass. Rinse and repeat. To take it to its most extreme, if you were writing a function that adds two integers, the test would first consist of one line: "let result = "add(5, 10);". Then, you write the add function with one line: "0". Next, you write the next line of the test: "asserteq!(result, 15);", and replace the 0 in the add function with a 15 (running tests between edits). Then, you write a new test that adds two different numbers together (say, 5 and 7). At that point, you finally replace the hard coded result in the add function with "a + b".

Then, you might add a test that detects what happens if you take the maximum value of an integer, then add one. In Rust, this will result in an overflow, which will cause a panic (and thus fail the test), so you then decide what you want the result to be - maybe you want to ensure that anything that would overflow returns the maximum value, for example, before adding more code into the function that does so. Alternatively, you might decide that panicking is exactly what the code should do, and so you specify in the test that a panic is expected behaviour.

This is, of course, a pretty extreme example, just for illustration. The idea here is that you think about what should happen during routine use, write just enough of a test to fail, write just enough code to make the test pass, and keep on going until the expected behaviour is there and is guaranteed to work. Then, you think about the edge cases, write just enough of a test to fail in the edge case, then write the code to deal with the edge case. As a result of the extra work up front, you end up with a series of tests that both describe the expected behaviour of a piece of code, and ensure that the behaviour remains as expected, meaning that you don't end up with extra work later on when changing things might actually be more difficult.

Parker
Автор

It is great to see that you're making Rust videos again!

CGMossa
Автор

This video was exactly what I needed!
I do agree on the need of a hexagon pattern video, mostly so we can see how to write adapters for our tests.


Thanks for all your effort <3

marcusradell
Автор

Always supporting Rust Community ! Yeaa

jatmikoherjati
Автор

Thanks again for all the content on rust. I was wondering if you could do a hexagon pattern as you did for go but just for rust I think it would be a good topic.

learnityourself
Автор

Thanks Tensor, your videos is helping me a lot.
Regards from Brazil.

LassNoches
Автор

Correct me if I'm wrong but in 15:10 you couldn't have done v.iter().sum()?

patatasdelpapa
Автор

Thank you for the content, and looking forward further content on this topic thanks again

hubstrangers
Автор

Hi! Will you use Rust for embedded systems as well? Would love to see your videos!

yousufuyghur
Автор

Thanks for the great content! I have a question though: When writing tests for Struct methods, is there a way to avoid creating an instance of the Struct in the test function? It just doesn't feel right to do that

pheel_flex
Автор

is there any book for the TDD in Rust Sir? pls give the recommendation

tubex
Автор

Hi dear friend thank you so much for sharing this video

Stack_net
join shbcf.ru