Intro to Elixir | Lesson 22: Project Testing and documentation

Elixir
Transcript

English (Auto-generated)

Hello Friends. Let's continue our Star Wars api integration project And today we're gonna focus on writing running tests and on documentation. So if you recall when we generated the project using mix it automatically generated this test folder with it That has two files. One is the test helper which will really contain any kind of configuration code that we will need for our tests. In this case it's really starting the testing framework E X unit and remember E X unit is built in with elixir so you do not need to import any special library or anything extra. Um And there's also this Star Wars test. So a couple of things I'd like to note for this one, one is like you know, it's good to name it like the module. So Star Wars Star Wars but you appended with underscore test and you name the module accordingly. So the module will have Star Wars tests and also note that the file name must be E X s and if you recall the excess I've added here, a bit of notes, E X Xr, elixir script files they don't get compiled but you can execute them like elixir code. So the code in the excess is electric code, they just don't get compiled with your main application which is exactly what we want with testing with test files. We do not need really need those test files too be compiled. We don't need them to be part of our application. We just need to run them in development or sometimes in a continuous integration server but we don't really need them to be part of our application, they could be just extra weight, that is not needed in the in a live application. Alright, so the first thing here, you know, is there is this use e x unit but case and there is this thing here that looks, you know, a little bit maybe odd for you right there. Okay. So these are things that haven't actually covered. Um this is what you would call a macro. So this use here is what you would call an elixir. Macros. Elixir. Macros are constructs that can kind of give you extra functionality and I intentionally did not cover it in this course because I mean a couple of things. One is, I wanted this to be more of an introductory course and number two is even in, you know, as you learn more about elixir, macros are usually used as a last resort. Like you should just try to work with modules and functions whenever possible, but in some cases, especially when you're designing kind of custom libraries like this, built in test libraries, then macros can come in extremely handy. So again, this test here is a macro and to import basically to allow module to inject a lot of its code and macros into into it. Then we have this use which is again similar to import um but you know, it doesn't involve having macros and such. So this is why I didn't go through it when we talked about using alias and import. Um so I would say, you know, in my opinion, until you learn about macros, you know, it's relatively, I'd say straightforward to learn how to write this code. So go ahead and write how to learn how to write this code and then you will learn how it can work under the hood using macros. So again, to have the testing framework available in this module, we have to write this use e x unit dot case and we have this one here, I'll explain it to you later which is called dark test when I talk about documentation and testing with documentations awesome. So this um test right here takes inner strength so it's important that that string just describes what you're really testing. Okay. But you're really testing and then you open a block here with the duende and you write your test scenario right here. So you do any setups, you need, you need you and then at the very end you should have some type of either an assert or refuse that something must be equal to something or something must not be equal to something. Okay, test you know, honestly this is probably, you know 60 to 70% of all test cases that you would write in elixir lecture has this nice property that a lot of the things are written as functions ideally pure functions, which means, you know, as long as you know what to input to a function and then know what to expect as an output for the function you should be able to write this relatively easily. So what this is saying here, Well, assert that when I call the module Star Wars and I call the function Hello in it, I get this atom, this is worth this double equal. So I'm asserting that this here is double equals to that. So to run this test, you simply type mixed test which will run all of your tests in your application. So it says one dark test, which I'll explain later through this dark test wars. One test, this is a test here and then zero failures. So if I go to the code Star wars and I put worlds instead of words and then I run mixed test. Notice that the test fails. Okay? The test fails. So it says the left is world while the right is world. We're expecting it to be world. But the left, which is this is the left, right here gave us worlds while we expected the world atom. So this is why the test fails. And of course even the dark test fails as well, which I'll explain, explain it next. So maybe you know, to learn a little bit more about this testing. Let's go ahead and test this other function that we've written here, which is the greeting. Okay, so let's go ahead and write tests for greeting. So I'll go ahead and type test it greets by returning Hello? Who's tried to lies, string that is so you give it a string, it fertilizes it and then it uh puts hello at the beginning of it so I can say something like this. A search if I call star wars dot reading and I pass it something like john I should get as my output Hello on smith Now, regardless of whether you're doing disturbing development or not, I, I won't get into the debate on whether you should or you shouldn't. I would say, you know, I'd leave this for another time and place, but I would say always make sure that the test fails under some conditions. So don't just accept that the test passes. So I commented out the code here and then notice here the left, which is what the function returns is now because again it's empty definition so it returns now what I expected it to return. Hello john smith. Okay. So um, I go back to my code, Mayan comment and then I run my tests and now I have two tests and the original dark test here and the test passes. Okay, so the test passes right here, awesome. So again, it's really as simple as that writing tests in elixir again, in some cases you need to do setups, you need to put the proper data structures, you need to do with the ins and outs but as you can tell here, um um it's as simple as doing asserts and then calling a function and expecting some kind of an output. So if you recall here, we have helpers, strings here. Um and we have this title is that we brought in from the from the from the last lesson. So let let let me show you how we can go ahead and test. So this again, just repeat the whole process for you. So I'll follow the same folder structures, output helpers and then I will put the file name, I'll call it the same as the module name. Strings. But remember we append underscore test to it and then we put E X S as the extension because we wanted to be a script death module helpers. That strings. That's new. And then we have used E x unit dot um I think just in case and then you can say it just it just idolize this. Thanks. Yes and we do here. Um we could use aliases this, considering we're likely going to be using the module over and over. Which means instead of having to call helpers dot strings every time I can simply call strings so strings dot and then notice my auto complete and bs code already shows us method vitalized. So I say I served that five paths. Hello world good. I should get back. Hello. And maybe again just to make sure that the test fails under some conditions, I'll just comment out the last one and then run my test. If I don't put this in on the joint, I'm just gonna have the list instead of the string. So it says, well I get the list that have hello and world while the right is hello world. So Ryan comment this and then I ran it again and now my desk passes. Okay. So this is how we basically added a test for this string helper and then we added also a test for for our greeting. Awesome. So next up here, let me show you documentation and testing with documentation. So back to my star wars um, the module that is kind of the default one generated by the mixed command notice that it has this a doctor strength block right here and there's two things here I want to show you here. So uh huh doc is a way for you to document your functions. So later you could have full documentation and you could even have like a nice display for it on a web page if you want to and such. So that's why, especially if you're building a library. This comes in very, very handy. So a dog, I could say this function agreed by returning Hello with title ized, vitalizing the import and what's super interesting here is this example that you can add right there actually acts like a test scenario as well. So I could say I e X star wars dot reading. Hey john miller. Okay I should get back. Hello again. Hello. So if I run this notice here I have one dog test. Three tests. Tests Too Dark Tests three Tests. So by just adding an example, not only you're adding an example to your documentation which would be very handy for the reader of your documentation, but also you have a test that gets executed. So if this basically fails later, the dark test will fail as well. So notice not only the test we've written but also the dark test in some cases. I mean in this case you may be wondering well um like why do we have both? Well, I mean honestly you could probably just do with um with only um the um you may even skip this test altogether considering you have a dark test. So you can skip this one because it's really just doing the same. But usually test can be more elaborate because you're a little bit limited with what you can put in elixir tests. But when you actually have this um desk block right here, you could you could do a lot more elaborate setups and so on. All right. So let's now go ahead and do the same thing for this one here. So I'll go and put talk this function idolizes strings by capitalizing each work. You can say examples. I E X. Strings relies on the world return Hello world. Now this won't execute by default. Just I want to show you this still we have only two dark tests and three tests. The reason is remember we have to put this for this to being able to have to put dog test and then we have to give it. Um in this case we could say strings Makes tests. Now we have three doctors and three tests. Also remember while we're on the topic of documentation, you could document functions using adblock but you could also document the modules and you should at least have module documentation, which is I think a little bit more important by having at module doc. This module provides you know, Andy's turn helpers. So we've added it last time. So this is basically between module dog and dog. Also, you could have really good documentation for your electoral project. This will be it for today's lesson. Thank you for joining. And I'll see you next time.
196 Views 0 Likes 0 Comments

We learn how to write and run tests in Elixir projects. We also learn how we document functions.

Comment
Leave a comment (supports markdown format)