Advanced Testing in Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

It's good. But I'd like to see testing something more complicated than basic math functions. Testing using sub tests and test setup and tear down. Also how to mock out databases or other external services (not necessarily http, which you showed already).

My needs are simple.

👍︎︎ 16 👤︎︎ u/vm0661 📅︎︎ Feb 28 2019 🗫︎ replies

Using build tags for separating out the types of tests works out very well. We are using that for a production Go code base at my work and it allows us to have our different levels of testing all in the same depot.

👍︎︎ 3 👤︎︎ u/chumpMcGump 📅︎︎ Feb 28 2019 🗫︎ replies

Perfect timing, I just wrote my first tests yesterday so time to see what it can do

👍︎︎ 1 👤︎︎ u/CactusGrower 📅︎︎ Feb 28 2019 🗫︎ replies

Getting errors for the playback ID. Did you remove the video from your account?

👍︎︎ 1 👤︎︎ u/________null________ 📅︎︎ Feb 28 2019 🗫︎ replies

Saving this for later.

👍︎︎ 1 👤︎︎ u/enby-girl 📅︎︎ Feb 28 2019 🗫︎ replies
Captions
[Music] hi everyone is elect from Satori alleged owner and enesta to do we're gonna be taking a look at a selection of more advanced testing practices used by the likes of the cool core language developers and in popular production level tools this is going to be a continuation of my previous tutorial which was a gentle introduction to testing angle and the full text version of this tutorial can also be found my website and I'll leave a link to that and the description below now just to get started I've already filled in the official Galang repository which contains all of the source code for the language and again I'll be leaving a link to this in the description below now we're gonna start our journey and the strings package so if you open up the source directory and then scroll down into strings now the first taste the broken a look at is the table driven taste approach and if you open up the strings dot underscore testicle we'll be able to see some really good examples of how they've used this to ensure the validity of their code now I should quickly mention that any of the techniques that we cover within this tutorial can and should be used within your own production level projects now testing is something we often forget or don't have time to implement and I feel that's a poor choice made by the developer as in the long run if you have an adequate testing framework arrange your projects you can in fact save time by knowing that any changes you make aren't going to subsequently change behavior further than your programs it gives you that confidence that when you really send to production everything is going to work as intended ok so that's probably enough from me on the theory side of things let's jump in and see how the go developers actually test their code now the first technique that we're going to cover is the table driven approach now as you can see here the go developers have specified new struct called index text and Ness contains a string a separator and an a value if then declared an array populated with a number of objects or strokes and each of these contains the starting string the separator and the expected value now what happens in this situation as within one of the tests and I believe is the last index so index test here our last index that's test is then subsequently run and Esther eats through every test case with an re and validate is that the actual output is equal to the expected output and if it's not then it throws an error and the test fails okay so in order to give you an idea as to how you would implement this yourself let's jump into a really simple cool base project now I'm gonna create a really simple add function like so which is gonna take n and x and a y both of type end and return an end and within here is simply gonna add both of them never then are mndot underscore test go fail we're gonna do the following so type add result or struct and this is gonna have an X or type end Y of type int and unexpected a gain of tape and now just below it we're going to want to do a test add nest is gonna take in a pointer to testing now we're gonna want to specify an array of expected results so to do that we're going to add results equals the array of add result and within here we're going to start off by doing one plus one is two nice and simple now within our taste add function we're gonna want to iterate over every object within our add result array and to do that we can do for underscore test and range add results and we're gonna want to do result equals add test or X test our way and F the result does not equal test or expected we're gonna want to do t dot fatal you expected result not Kevin something along those lines save that and let's try running this test by going go test and as you can see everything passes okay and just to double-check let's show the report option which shows that our test ad function test ad has successfully run now the expectation for maturing systems is that more and more test results and test cases hope you added to this array as time passes you'll be able to exhaustively test all of the various inputs and see that the output matches your expectation okay so the next technique we are going to cover as the use of the taste data directory now in certain situations you won't be able to specify your expected input and output as an array of elements as we have done here you may be trying to test how you read and write to fails on the file system or how you parse proprietary data formats or whatever if this is the case then one option is to create a taste data directory and store any files you may need for testing with an add directory so if we jump back into the goal source code once again and we open up the archive package and the tar package within here you'll see an example of how you've used less tasty a directory and how they fold up with example tar files that they can subsequently use within their own testing so let's open up the reader underscore test I'll go file never then here the first example of this being used as within the taste reader function here now this will utter it through all of the fails within the tasty a directory and ensure that when it's unzipped any of the fails contained with the most heart rate and our tar archives have not been modified in any way so let's see how this would work and are really sample project now if we come in here and we create a new folder called test data and with tenant we're going to add test dot data just to prove a point hello world should be ante store data with our mean underscore test go fail I'm gonna want to create a new taste method so I'm gonna want to taste if we can appropriately read the data from that text idea fail so test read fail again what's poor pass on the pointer to testing dot T and with an S we want to do the followings with data or air equals IO you tell dot read fail and test data slash test dot data so it's a relative path here and that's important to know and let's just handle the error so f error does not equal null then we want to t dot fatal should not open fail blow this F we successfully read the fail then we want to verify that we've read the contents successfully so f string data does not equal hello world then again we want to t dot fatal string contents do not match expect it like so try runners with cool test and the verbose spike and you should see that everything runs as expected so we've been able to make use of this tasty at the rectory now the next section of this tutorial is going to be about mocking HTTP requests now as soon as you start writing production level API is and services it's more than likely you'll start interacting with other services and being able to test the way that you interact with these services is just as important as testing the other parts of your code base however you may be interacting with REST API is that perform crud actions on databases and as such you don't want these changes actually being committed to your database when you're just trying to test to see if things are working so in order to gather in this problem we can mock HTTP responses using the net slash HTTP slash HTTP test package which is our best friend and these situations let's start off by creating a new test so funk test HTP request and again on the pass on the pointer and just at the top we want to create a new handler object which is going to be func W HTTP response rater are the pointer to the HTTP request now with an S I simply want to do io2 rate string W and I'm gonna put in a really simple JSON response so status and remember to expect escape your quotes and say get status is good and close that off okay so the next thing we want to do is perform the request we would like so request equals HTTP test dot new request and this is going to be a get request and we'll say the URL is going to be my website so tutorial age Donaire and the last parameter will be nil so let's do W equals HTTP test dot new recorder and the handler will be W and request nice and simple so now that we've got our mocked response let's actually perform our requests so response because W dot result and the body of this or the error of equal oh you tell thought read all response dot body it's upper case now just below this we want to do the following so F say status code our F 200 does not equal response dot status code then let's do t dot fatal status could not okay like so and let's just quickly to FM t dot print line string body we run go tests you should see that it marks our requests to tutorial age dotnet and instead returns our JSON string which we can then use for further testing within the body so that would be typically done with an S section here now this could typically be things like asserting how you want your system to react when statuses are different from your standard good ok so the next thing we're gonna have a look at or the next technique we're gonna have a look at is the use of separate packages within your test Al's so if we open up the strings package once again and we open up the strings underscore test I'll go fail you see at the top that they've created that custom package which is strings underscore test and this is different from the package that you'll see the original strings source code but then now the main reason for this is to help avoid cyclic dependencies so in some scenarios you'll need to import a package into your star underscore test or go fail to adequately adequately write your tests however if the package you import is already in the package that you're trying to test you may see issues start to appear now the final thing I want to cover with the mess tutorial as the way you definitely your unit and your integration tests say for example you're writing tests for large enterprise gobei systems then you more than likely have a set of both integration and unit tests ensuring the velocity of your system more often than not however you'll find your integration tests taking far longer to run as opposed to your unit tests due to the fact that it could be reaching out to other systems in this case it makes sense to put your integration test into a wild card underscore integration underscore test or go fail so every comment to our simple example we would do something like this and at the top of our integration test fail we would do plus bailed integration now let's just add a really simple test to show this is working and the testing dot T let's to deafen t9 integration test past like so and the top of our unit test we may want to specify that less as a unit to the text so when we next go to test our systems we can specify that we only want to build our integration tests and the reason that wasn't building is because I cannot spell so let's try this again so go test specifying tags equals integration and as you can see integration test passed and everything is working as expected now we can then specify that we want to run our unit tests so tags equals unit and bull specified for both nuts as well as you can see all of the test cases that we've specified with an min underscore testicle have been successfully run so that is now all working as intended so just to wrap up this fairly long tutorial we've managed to look at some of the more advanced testing techniques and tracks employed by the go language maintainer now hopefully you found this useful and I give you the insight needed to go off and improve your own testing frameworks if you did find it useful or have any other further questions or tips of your own then please don't hesitate to let me know in the comment section below and as always please like the video and subscribe to my channel for more programming go and ten Cheers
Info
Channel: TutorialEdge
Views: 35,836
Rating: undefined out of 5
Keywords: programming, tutorials, coding tutorials, tutorialedge, go programming, golang programming, go testing tutorial, go advanced testing tutorial
Id: S1O0XI0scOM
Channel Id: undefined
Length: 16min 3sec (963 seconds)
Published: Fri Aug 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.