Transcript
This transcript was autogenerated. To make changes, submit a PR.
Hello everyone. Today I am talking a few minutes
for this important mission to introduce you to
this fantastic project, test containers.
So why I am so excited about test containers?
The answer is so simple. Test containers is a
framework that makes testing the dependencies of
your backend application really easy. And the most important
part of test containers is that everything is coded
in your favorite programming language.
If you want to use go, if you want to
use rust or closure, test container
is there for you. And of course, as the name suggests,
all the dependencies are simple containers.
Containers are easy to transport,
containers are easy to distribute,
immutable, and in fact, containers are easy to
deploy. So today I'm going to show you how
how to use test containers models and the sharing in
the cake. I will quickly show you how to create
your own test containers model. So let's
go. And a big question is,
what's a test containers model? So let's start
with a simple test containers models Redis Redis
is the world's fastest in memory database,
Redis IO. About models, there are
over 15 of them already created. For you
to find model, go to the model section on
the website just here and then select
its language. Go for example.
Then search for the model just
here, or type just into
the search bar. Sorry,
Redis. As you can see,
the redis model is available in several
languages. For example javago.net
js. Okay great.
So, okay, let's go back to
our code for the first time. It's time.
So as you can see, I just
close my terminal. As you can see,
I've got a little application that lets you
count the number of visitors to a webpage.
And for that I have HTTP server,
HTTP server here I have a root handler
with my business logic
to increment the counter and just get
the current number and just
print it. Okay, if we want to test that.
Okay, it's really easy
because you can run a docker container or just use
docker compose. So let's go multi macfly
back to the nineties with a simple contour
application. So normally this application connects to
my production redis instance, but I need to test the
function that increment the counter.
Well, it's precisely at this moment that I can use
the text containers model. And just for,
just for fun and to play with it.
Okay, you open a terminal here,
okay. And we go to the simple
model just here. And after that we
just run redis, sorry,
Redis. Redis in background.
And after that let's go for go.
Run main dot go. Okay,
not precisely important to build the application.
And after that, okay, let's go for curl,
the HTTP 323.
Okay, we have a counter. Great.
You have a sound like Tada. And if you
cut the application, okay, I stop the application
and run the application when you play with it.
Let's go. You have the counter because you use
a docker counter to
play with application. Sorry. Okay, so good.
But you know, it's not reliable
to use docker. So we
keep, we keep for example, redis in
background and now we discover the test.
So let's go to see how to use,
okay, I just radius my terminal.
Let's go to see how to use the redis model.
Okay. To use the redis container.
As you can see, it's really, really easy.
Just one line, it's really good.
So you can use the redis model. Redis model is here inside
test containers. Test containers, go package,
inside mole. And with the redis package and
you can run the container just like that.
So it's not necessary to precise the
image, but it's just an option.
After that we just
get the endpoint of my redisaber to
create a new client. Okay.
And finally we can use the increment function,
you remember the increment function here to test
my redis. So, okay, this example is so
trivial. No problem with that,
just a tip. I used
to set the counter to a value and to increment
to use a 20, for example, other value.
And let's go to execute this test.
Oh, magic. Okay, so it's
possible that I have a little bit
of cash about this, this test.
But as you can see, okay, we have here the
port of test containers. Okay. And finally
we have here just a port of
my test. So, okay.
And in this example, if you want, for example,
if I cannot change redis for mongodB, for example,
the test containers models exist. So it's really good.
Okay. It was the first, very first example.
So let's go for most
advanced example. Okay, I just
keep the first example here.
Sorry, just here, keep my guru
inside my first example.
Okay, close, close. Okay. Just keep this terminal.
I will explain what I want to keep
this part of my simple model
in my advanced, other model. I want to show
you just here the main function.
Exactly and precisely the same case as the first.
But in this case we use an
HTTP call to increment my counter.
For example, the advance is the second microservice.
To use the first simple microservice.
Okay, why use
this example for you today? Is just because I want to
demonstrate another amazing project and its
name is microx. Microx is awesome.
Microx is here, just here
in the test. So I will explain. I have
an HTTP server here with
root handler here and I use the
increment here. So just in this case I
print number of visitors with just
a number or the error because you
call another server. In this demonstration,
as you can see, I call the previous. So if I test this
second advanced model,
I can curl it with 4444.
I'm sorry, I'm sorry. I just go run
the main, let's go. And finally I
change directory for this and I can curl
here. And as you can see, we use the previous model
as a microservice. Okay, so it's
a big, big, big problem because if you
have for example 20 microservices,
it's really difficult to run
all around the world. So in
my example I will use the amazing
microx model just here.
The idea of microx is to create,
to put in place a contract testing between your microservices
and to keep up to date using
for example openapi. So in
my test data here, I have the description of
my API. So it's not a trivial
description because in this example
counter API, I have a root path and I
have a simple counter here. But the magic
here is that I use like
a script to generate
the increment. Okay? So it's like a mock.
And this URL for example is the same
as microx. So for example,
my test, I create,
I use the microx container here,
okay. I use the package, this good package
testify to,
to ensure that I don't have any error
here. Finally I import
as a main artifact. So I let you discover my
crux about that to define my,
finally my API. So in fact my mock
about API, I get
the endpoint of microcs and I
will use the rest mock endpoint to
have the ability to play
with the mock. And finally
I use increment with this mock a
first time and a second time to be sure that
my increment function as really
great. So let's go. And if
I run the test, as you can see, it's okay,
it's okay. So really good to demonstrate
the ability to use this amazing project.
Okay, so Julian, two amazing project
in one video. Yeah, because in the
day to day of software engineering, you have
to use the precious software,
the precious tools. And I think that test containers
and microcs are two of best in
my set. Okay, so really easy
to use as you can see. So now
let's go to create our own test container.
Okay. And the first question is how
to create my own test containers models. What's next?
Now it's time to create your own container.
Okay. Since for me it's late and I have a little
imagination, we're going to create an Nginx
model, okay? So let's go.
And to create a model, it's very, very simple.
Okay, let's go. To create a model,
you just declare a go package with
a primary function like
run container. Okay. It's more like a convention.
It's more like a convention.
Here you're running always
the context is used to
create your container and some customizers.
So some options about. So in
this example we have a first container request.
After that we use image property
and we can declare exposed ports
property. And you have a
lot of option to say to test
container. Okay, the container is ready,
the container restart. So after
that you create a generic request.
And here you can use started with
value to true to say. Okay, start now.
Because if you want to prepare, for example,
you can create a run container without the started
true. But if your model's users
use it without started,
the model is not started. So easy.
After that you can create a generic container.
And as you can see, it's really easy inside
test containers to use the generic containers. You know,
it's not precisely important to,
you don't need, in fact, to create all containers.
You can use just vanilla test containers
with generators, generic containers. In this example,
I use the example of the official website of
test containers to retrieve the host and
to get the map port.
In our case, it's this port here and
to put the URI inside
the destruction. Yeah, easy to use for
the rest of
this demonstration. So here,
for example, you can create some
options and in my case, I use with index,
with auth file index to replace
the default index of NgInX.
Okay. And test containers is very,
is very cool in fact, because you have all
the sets about, about you need.
For example, if you want to put files inside the containers,
if you want to map some ports,
etc. Etc. So, okay, we have
our first model, okay, in your imagination
you can use this model with this full path.
For example, go get blah blah blah blah blah model nginx.
Okay. But in our case we use a test
file to demonstrate how to use this.
Let's go to see our test. This test is a
test run container, okay, basic. And we
just run the function
run container, sorry, with a specific index.
My index is really,
really good because you have inside just conv.
42, okay. I'm sorry,
I closed the test. We check
that we don't have no error. We check also
the cleanup of the container. And finally
we fetch the value of nginx
to be sure that it's equal to
con 42. So, okay,
I just put this comment here.
The value con 42 comes from test
data index HTML. And I use
a tiny function, fetch to get the
nginx server. Okay, let's go to run the tests.
Okay, so if you want to practice like
BDD or other things like that. My first error
was here. As you can see,
it's okay. We have a bad test.
Okay, I just put the initial value.
Okay. So like we said,
et voila. Know you know how to use test
container very quickly.
If you'd like more information about test containers,
please don't hesitate to get in touch with the community.
They are really nice. I'd also
like to thank them for this lovely t
shirt and in fact, this amazing
technology. Because I think that in the software engineering,
this is precisely a tool, a missing
tool. But now it's good to
have test containers. And one
more serious note, don't hesitate to simply test your go applications.
It's really important and very easy.
And now you can use a bunch of tools
about AI. So see you next time
for new adventure.
And really happy to have you today.