You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/manuals/testcontainers/getting-started/go.md
+16-18Lines changed: 16 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,9 +22,9 @@ From the [Go Release Policy](https://go.dev/doc/devel/release#policy):
22
22
23
23
> Each major Go release is supported until there are two newer major releases. For example, Go 1.5 was supported until the Go 1.7 release, and Go 1.6 was supported until the Go 1.8 release. We fix critical problems, including critical security problems, in supported releases as needed by issuing minor revisions (for example, Go 1.6.1, Go 1.6.2, and so on).
24
24
25
-
_Testcontainers for Go_ is tested against those two latest Go releases, therefore we recommend using any of them.
25
+
_Testcontainers for Go_ is tested against those two latest Go releases, therefore it's recommended to use any of them.
26
26
27
-
## Step 1:Install_Testcontainers for Go_
27
+
## Step 1:Install _Testcontainers for Go_
28
28
29
29
_Testcontainers for Go_ uses [go mod](https://blog.golang.org/using-go-modules) and you can get it installed via:
30
30
@@ -66,28 +66,26 @@ look.
66
66
67
67
*`Image` is the Docker image the container starts from.
68
68
*`ExposedPorts` lists the ports to be exposed from the container.
69
-
*`WaitingFor` is a field you can use to validate when a container is ready. It
70
-
is important to get this set because it helps to know when the container is
71
-
ready to receive any traffic. In this case, we check for the logs we know come
72
-
from Redis, telling us that it is ready to accept requests.
69
+
*`WaitingFor` is a field you can use to validate when a container is ready.
70
+
It is important to get this set because it helps to know when the container is
71
+
ready to receive any traffic. In this case, the project checks for the logs that come from Redis, mentioning that it is ready to accept requests.
73
72
74
73
When you use `ExposedPorts` you have to imagine yourself using `docker run -p
75
-
<port>`. When you do so, `dockerd` maps the selected `<port>` from inside the
74
+
<port>`. When you do so, `dockerd` maps the selected `<port>` from inside the
76
75
container to a random one available on your host.
77
76
78
-
In the previous example, we expose `6379` for `tcp` traffic to the outside. This
77
+
In the previous example, the `6379/tcp` port is exposed to the outside. This
79
78
allows Redis to be reachable from your code that runs outside the container, but
80
-
it also makes parallelization possible because if you add `t.Parallel` to your
81
-
tests, and each of them starts a Redis containereach of them will be exposed on a
82
-
different random port.
79
+
it also makes parallel execution possible: if you add `t.Parallel` to your
80
+
tests, and each of them starts a Redis container, then each of them will be
81
+
exposed on a different random port.
83
82
84
-
`testcontainers.GenericContainer` creates the container. In this example we are
85
-
using `Started: true`. It means that the container function will wait for the
83
+
`testcontainers.GenericContainer` creates the container. This example uses `Started: true`. It means that the container function will wait for the
86
84
container to be up and running. If you set the `Start` value to `false` it won't
87
85
start, leaving to you the decision about when to start it.
88
86
89
87
All the containers must be removed at some point, otherwise they will run until
90
-
the host is overloaded. One of the ways we have to clean up is by deferring the
88
+
the host is overloaded. You can do the clean up by deferring the
91
89
terminated function: `defer testcontainers.TerminateContainer(redisC)` which
92
90
automatically handles nil container so is safe to use even in the error case.
93
91
@@ -96,11 +94,11 @@ automatically handles nil container so is safe to use even in the error case.
96
94
> Look at [features/garbage_collector](https://golang.testcontainers.org/features/garbage_collector/) to know another
97
95
> way to clean up resources.
98
96
99
-
## Step 3: Make your code talk to the container
97
+
## Step 3: Talking to the container
100
98
101
99
This is just an example, but usually Go applications that rely on Redis are
102
100
using the [redis-go](https://github.com/go-redis/redis) client. This code gets
103
-
the endpoint from the container we just started, and it configures the client.
101
+
the endpoint from the container that was started before, and it configures the client.
### Step 2: Get Testcontainers to run a Redis container during our tests
77
+
### Step 2: Run a Redis container in the tests
78
78
79
-
Simply add the following to the body of our test class:
79
+
Simply add the following to the body of your test class:
80
80
81
81
```java
82
82
@Rule
@@ -85,22 +85,22 @@ public GenericContainer redis = new GenericContainer(DockerImageName.parse("redi
85
85
```
86
86
87
87
The `@Rule` annotation tells JUnit to notify this field about various events in the test lifecycle.
88
-
In this case, our rule object is a Testcontainers `GenericContainer`, configured to use a specific Redis image from Docker Hub, and configured to expose a port.
88
+
In this case, the rule object is a Testcontainers `GenericContainer`, configured to use a specific Redis image from Docker Hub, and configured to expose a port.
89
89
90
-
If we run our test as-is, then regardless of the actual test outcome, we'll see logs showing us that Testcontainers:
90
+
If you run your test as-is, then regardless of the actual test outcome, you'll see logs showing you that Testcontainers:
91
91
92
-
* was activated before our test method ran
93
-
* discovered and quickly tested our local Docker setup
92
+
* was activated before the test method ran
93
+
* discovered and quickly tested the local Docker setup
94
94
* pulled the image if necessary
95
95
* started a new container and waited for it to be ready
96
96
* shut down and deleted the container after the test
97
97
98
-
### Step 3: Make sure our code can talk to the container
98
+
### Step 3: Make sure the code can talk to the container
99
99
100
-
Before Testcontainers, we might have hardcoded an address like `localhost:6379` into our tests.
100
+
Before Testcontainers, you might have hard-coded an address like `localhost:6379` into your tests.
101
101
102
102
Testcontainers uses *randomized ports* for each container it starts, but makes it easy to obtain the actual port at runtime.
103
-
We can do this in our test `setUp` method, to set up our component under test:
103
+
You can do this in your test `setUp` method, to set up your component under test:
104
104
105
105
```java
106
106
String address = redis.getHost();
@@ -112,16 +112,14 @@ underTest = new RedisBackedCache(address, port);
112
112
113
113
> [!TIP]
114
114
>
115
-
> Notice that we also ask Testcontainers for the container's actual address with `redis.getHost();`,
115
+
> Notice that the previous code also asks Testcontainers for the container's actual address with `redis.getHost();`,
116
116
> rather than hard-coding `localhost`. `localhost` may work in some environments but not others - for example it may
117
117
> not work on your current or future CI environment. As such, **avoid hard-coding** the address, and use
118
118
> `getHost()` instead.
119
119
120
120
### Step 4: Run the tests
121
121
122
-
That's it!
123
-
124
-
Let's look at our complete test class to see how little we had to add to get up and running with Testcontainers:
122
+
Let's look at the complete test class to see how little you had to add to get up and running with Testcontainers:
125
123
126
124
```java
127
125
publicclassRedisBackedCacheIntTest {
@@ -158,8 +156,8 @@ public class RedisBackedCacheIntTest {
158
156
159
157
It's easy to add Testcontainers to your project - let's walk through a quick example to see how.
160
158
161
-
Let's imagine we have a simple program that has a dependency on Redis, and we want to add some tests for it.
162
-
In our imaginary program, there is a `RedisBackedCache` class which stores data in Redis.
159
+
Let's imagine you have a simple program that has a dependency on Redis, and you want to add some tests for it.
160
+
In your imaginary program, there is a `RedisBackedCache` class which stores data in Redis.
163
161
164
162
You can see an example test that could have been written for it (without using Testcontainers):
165
163
@@ -185,8 +183,8 @@ public class RedisBackedCacheIntTestStep0 {
185
183
```
186
184
187
185
Notice that the existing test has a problem - it's relying on a local installation of Redis, which is a red flag for test reliability.
188
-
This may work if we were sure that every developer and CI machine had Redis installed, but would fail otherwise.
189
-
We might also have problems if we attempted to run tests in parallel, such as state bleeding between tests, or port clashes.
186
+
This may work if you were sure that every developer and CI machine had Redis installed, but would fail otherwise.
187
+
You might also have problems if you attempted to run tests in parallel, such as state bleeding between tests, or port clashes.
190
188
191
189
Let's start from here, and see how to improve the test with Testcontainers:
### Step 2: Get Testcontainers to run a Redis container during our tests
231
+
### Step 2: Get Testcontainers to run a Redis container during the tests
234
232
235
-
First, you'll need to annotate the test class with `@Testcontainers`. Furthermore, add the following to the body of our test class:
233
+
First, you'll need to annotate the test class with `@Testcontainers`. Furthermore, add the following to the body of the test class:
236
234
237
235
```java
238
236
@Container
@@ -241,22 +239,22 @@ public GenericContainer redis = new GenericContainer(DockerImageName.parse("redi
241
239
```
242
240
243
241
The `@Container` annotation tells JUnit to notify this field about various events in the test lifecycle.
244
-
In this case, our rule object is a Testcontainers `GenericContainer`, configured to use a specific Redis image from Docker Hub, and configured to expose a port.
242
+
In this case, the rule object is a Testcontainers `GenericContainer`, configured to use a specific Redis image from Docker Hub, and configured to expose a port.
245
243
246
-
If we run our test as-is, then regardless of the actual test outcome, we'll see logs showing us that Testcontainers:
244
+
If you run your test as-is, then regardless of the actual test outcome, you'll see logs showing you that Testcontainers:
247
245
248
-
* was activated before our test method ran
249
-
* discovered and quickly tested our local Docker setup
246
+
* was activated before the test method ran
247
+
* discovered and quickly tested the local Docker setup
250
248
* pulled the image if necessary
251
249
* started a new container and waited for it to be ready
252
250
* shut down and deleted the container after the test
253
251
254
-
### Step 3: Make sure our code can talk to the container
252
+
### Step 3: Make sure the code can talk to the container
255
253
256
-
Before Testcontainers, we might have hardcoded an address like `localhost:6379` into our tests.
254
+
Before Testcontainers, you might have hard-coded an address like `localhost:6379` into your tests.
257
255
258
256
Testcontainers uses *randomized ports* for each container it starts, but makes it easy to obtain the actual port at runtime.
259
-
We can do this in our test `setUp` method, to set up our component under test:
257
+
You can do this in your test `setUp` method, to set up your component under test:
260
258
261
259
```java
262
260
String address = redis.getHost();
@@ -268,7 +266,7 @@ underTest = new RedisBackedCache(address, port);
268
266
269
267
> [!TIP]
270
268
>
271
-
> Notice that we also ask Testcontainers for the container's actual address with `redis.getHost();`,
269
+
> Notice that the previous code also asks Testcontainers for the container's actual address with `redis.getHost();`,
272
270
> rather than hard-coding `localhost`. `localhost` may work in some environments but not others - for example it may
273
271
> not work on your current or future CI environment. As such, **avoid hard-coding** the address, and use
274
272
> `getHost()` instead.
@@ -284,9 +282,7 @@ current environment. Set `disabledWithoutDocker` to `true`.
284
282
285
283
### Step 5: Run the tests
286
284
287
-
That's it!
288
-
289
-
Let's look at our complete test class to see how little we had to add to get up and running with Testcontainers:
285
+
Let's look at the complete test class to see how little you had to add to get up and running with Testcontainers:
290
286
291
287
```java
292
288
@Testcontainers
@@ -324,8 +320,8 @@ public class RedisBackedCacheIntTest {
324
320
325
321
It's easy to add Testcontainers to your project - let's walk through a quick example to see how.
326
322
327
-
Let's imagine we have a simple program that has a dependency on Redis, and we want to add some tests for it.
328
-
In our imaginary program, there is a `RedisBackedCache` class which stores data in Redis.
323
+
Let's imagine you have a simple program that has a dependency on Redis, and you want to add some tests for it.
324
+
In your imaginary program, there is a `RedisBackedCache` class which stores data in Redis.
329
325
330
326
You can see an example test that could have been written for it (without using Testcontainers):
331
327
@@ -352,8 +348,8 @@ class RedisBackedCacheIntTestStep0 extends Specification {
352
348
```
353
349
354
350
Notice that the existing test has a problem - it's relying on a local installation of Redis, which is a red flag for test reliability.
355
-
This may work if we were sure that every developer and CI machine had Redis installed, but would fail otherwise.
356
-
We might also have problems if we attempted to run tests in parallel, such as state bleeding between tests, or port clashes.
351
+
This may work if you were sure that every developer and CI machine had Redis installed, but would fail otherwise.
352
+
You might also have problems if you attempted to run tests in parallel, such as state bleeding between tests, or port clashes.
357
353
358
354
Let's start from here, and see how to improve the test with Testcontainers:
### Step 2: Get Testcontainers to run a Redis container during our tests
382
+
### Step 2: Get Testcontainers to run a Redis container during the tests
387
383
388
384
Annotate the Spock specification class with the Testcontainers extension:
389
385
@@ -392,7 +388,7 @@ Annotate the Spock specification class with the Testcontainers extension:
392
388
class RedisBackedCacheIntTest extends Specification {
393
389
```
394
390
395
-
And add the following field to the body of our test class:
391
+
And add the following field to the body of the test class:
396
392
397
393
```groovy
398
394
GenericContainer redis = new GenericContainer<>("redis:6-alpine")
@@ -401,20 +397,20 @@ GenericContainer redis = new GenericContainer<>("redis:6-alpine")
401
397
402
398
This tells Spock to start a Testcontainers `GenericContainer`, configured to use a specific Redis image from Docker Hub, and configured to expose a port.
403
399
404
-
If we run our test as-is, then regardless of the actual test outcome, we'll see logs showing us that Testcontainers:
400
+
If you run the test as-is, then regardless of the actual test outcome, you'll see logs showing you that Testcontainers:
405
401
406
-
* was activated before our test method ran
407
-
* discovered and quickly tested our local Docker setup
402
+
* was activated before the test method ran
403
+
* discovered and quickly tested the local Docker setup
408
404
* pulled the image if necessary
409
405
* started a new container and waited for it to be ready
410
406
* shut down and deleted the container after the test
411
407
412
-
### Step 3: Make sure our code can talk to the container
408
+
### Step 3: Make sure the code can talk to the container
413
409
414
-
Before Testcontainers, we might have hardcoded an address like `localhost:6379`into our tests.
410
+
Before Testcontainers, you might have hard-coded an address like `localhost:6379`in the tests.
415
411
416
412
Testcontainers uses *randomized ports* for each container it starts, but makes it easy to obtain the actual port at runtime.
417
-
We can do this in our test `setup` method, to set up our component under test:
413
+
You can do this in your test `setup` method, to set up your component under test:
418
414
419
415
```groovy
420
416
String address = redis.host
@@ -426,16 +422,14 @@ underTest = new RedisBackedCache(address, port)
426
422
427
423
> [!TIP]
428
424
>
429
-
> Notice that we also ask Testcontainers for the container's actual address with `redis.containerIpAddress`,
425
+
> Notice that the previous code also asks Testcontainers for the container's actual address with `redis.containerIpAddress`,
430
426
> rather than hard-coding `localhost`. `localhost` may work in some environments but not others - for example it may
431
427
> not work on your current or future CI environment. As such, **avoid hard-coding** the address, and use
432
428
> `containerIpAddress` instead.
433
429
434
430
### Step 4: Run the tests
435
431
436
-
That's it!
437
-
438
-
Let's look at our complete test class to see how little we had to add to get up and running with Testcontainers:
432
+
Let's look at the complete test class to see how little you had to add to get up and running with Testcontainers:
0 commit comments