1 00:00:00,510 --> 00:00:03,120 In this video, I will show you how to 2 00:00:03,120 --> 00:00:06,029 write and run tests on your code. In a 3 00:00:06,029 --> 00:00:08,010 real project we would have spent a 4 00:00:08,010 --> 00:00:09,780 considerable amount of time writing 5 00:00:09,780 --> 00:00:11,969 tests for our code, to make sure that it 6 00:00:11,969 --> 00:00:14,759 functioned like we expected it to. Let me 7 00:00:14,759 --> 00:00:16,890 show you how to do that now. Let's write 8 00:00:16,890 --> 00:00:19,230 a test to make sure that our as time 9 00:00:19,230 --> 00:00:21,720 method for our time output trait 10 00:00:21,720 --> 00:00:24,510 functions like we expect it to. I'm going 11 00:00:24,510 --> 00:00:26,460 to start by creating an inline sub 12 00:00:26,460 --> 00:00:28,920 module named tests, it could be named 13 00:00:28,920 --> 00:00:30,960 anything as long as it has this 14 00:00:30,960 --> 00:00:34,320 attribute, config test, that means that 15 00:00:34,320 --> 00:00:37,380 this module only compiled during tests. 16 00:00:37,380 --> 00:00:39,630 So when we're running our release code, 17 00:00:39,630 --> 00:00:43,380 this isn't even in the binary. Next since 18 00:00:43,380 --> 00:00:46,290 this is a sub module, we'll need to use 19 00:00:46,290 --> 00:00:49,020 from super our time output trait, 20 00:00:49,020 --> 00:00:51,620 otherwise we won't be able to see it. 21 00:00:51,620 --> 00:00:54,360 Next we need a function to do the actual 22 00:00:54,360 --> 00:00:56,610 testing, let's just call it as time 23 00:00:56,610 --> 00:00:59,309 format, the name doesn't matter, again we 24 00:00:59,309 --> 00:01:01,140 just need to have an attribute. As long 25 00:01:01,140 --> 00:01:02,640 as we have the test attribute on our 26 00:01:02,640 --> 00:01:06,420 function it will be run as a test. Let's 27 00:01:06,420 --> 00:01:10,259 test a variety of inputs at once, so I'll 28 00:01:10,259 --> 00:01:13,079 create a vector and inside the vector 29 00:01:13,079 --> 00:01:16,229 I'll put tuple pairs, and for each tuple 30 00:01:16,229 --> 00:01:19,469 the first member will be a u 64 to use 31 00:01:19,469 --> 00:01:23,009 this input, and then a string to test the 32 00:01:23,009 --> 00:01:25,560 output. Then we just copy and paste this 33 00:01:25,560 --> 00:01:28,170 several times, and we have our data to 34 00:01:28,170 --> 00:01:30,929 work with. Of course, we need to set these 35 00:01:30,929 --> 00:01:32,969 all to different values, so let's set 36 00:01:32,969 --> 00:01:34,979 this to one minute which is 60 seconds, 37 00:01:34,979 --> 00:01:37,709 and then let's choose a hundred and 38 00:01:37,709 --> 00:01:41,670 fifty four and 3603, 39 00:01:41,670 --> 00:01:44,310 that'll test multiple minutes, and over 40 00:01:44,310 --> 00:01:48,090 an hour, 154 is two minutes and 34 41 00:01:48,090 --> 00:01:51,299 seconds, and 3603 is a one hour in three 42 00:01:51,299 --> 00:01:54,749 seconds. Now we can go write our loop. So 43 00:01:54,749 --> 00:02:00,229 for input and output in pairs, we assert 44 00:02:00,229 --> 00:02:03,200 equal and now you can call our function, 45 00:02:03,200 --> 00:02:07,560 as time, and let's also do as str so it 46 00:02:07,560 --> 00:02:09,378 will compare with the Equality operator, 47 00:02:09,378 --> 00:02:13,079 and put in our output and that's it that 48 00:02:13,079 --> 00:02:14,240 will loop through each of these 49 00:02:14,240 --> 00:02:17,150 to make sure that we've got the rest of 50 00:02:17,150 --> 00:02:20,120 this looking okay. That's all good, 51 00:02:20,120 --> 00:02:23,480 let's go run our tests. Running them is 52 00:02:23,480 --> 00:02:25,820 easy, it's just a matter of Cargo tests 53 00:02:25,820 --> 00:02:28,490 that will build everything in test mode 54 00:02:28,490 --> 00:02:31,430 and then run it, and there we go, so we 55 00:02:31,430 --> 00:02:34,220 can see that we've run our one test as 56 00:02:34,220 --> 00:02:37,340 time format which got an okay, we also 57 00:02:37,340 --> 00:02:40,430 ran doc tests which are tests embedded 58 00:02:40,430 --> 00:02:42,290 and documentation of which we have zero, 59 00:02:42,290 --> 00:02:45,950 because that is next. In the next video, I 60 00:02:45,950 --> 00:02:50,950 will show you how to document your code.