@@ -31,7 +31,7 @@ func TestNginxLatestReturn(t *testing.T) {
31
31
if err != nil {
32
32
t.Error (err)
33
33
}
34
- defer nginxC.Terminate (ctx)
34
+ defer nginxC.Terminate (ctx, t )
35
35
ip , err := nginxC.GetIPAddress (ctx)
36
36
if err != nil {
37
37
t.Error (err)
@@ -46,39 +46,43 @@ This is a simple example, you can create one container in my case using the
46
46
` nginx ` image. You can get its IP ` ip, err := nginxC.GetIPAddress(ctx) ` and you
47
47
can use it to make a GET: ` resp, err := http.Get(fmt.Sprintf("http://%s", ip)) `
48
48
49
- To clean your environment you can defer the container termination ` defer nginxC.Terminate(ctx) ` .
49
+ To clean your environment you can defer the container termination `defer
50
+ nginxC.Terminate(ctx, t)` . ` t` is ` * testing.T` and it is used to notify is the
51
+ ` defer ` failed marking the test as failed.
50
52
51
53
You can build more complex flow using envvar to configure the containers. Let's
52
54
suppose you are testing an application that requites redis:
53
55
54
56
``` go
55
- ctx := context.Background ()
56
- redisC , err := testcontainer.RunContainer (ctx, " redis" , testcontainer.RequestContainer {
57
- ExportedPort : []string {
58
- " 6379/tpc" ,
59
- },
60
- })
61
- if err != nil {
62
- t.Error (err)
63
- }
64
- defer redisC.Terminate (ctx)
65
- redisIP , err := redisC.GetIPAddress (ctx)
66
- if err != nil {
67
- t.Error (err)
68
- }
57
+ func TestRedisPing (t testing .T ) {
58
+ ctx := context.Background ()
59
+ redisC , err := testcontainer.RunContainer (ctx, " redis" , testcontainer.RequestContainer {
60
+ ExportedPort: []string {
61
+ " 6379/tpc" ,
62
+ },
63
+ })
64
+ if err != nil {
65
+ t.Error (err)
66
+ }
67
+ defer redisC.Terminate (ctx, t)
68
+ redisIP , err := redisC.GetIPAddress (ctx)
69
+ if err != nil {
70
+ t.Error (err)
71
+ }
69
72
70
- appC , err := testcontainer.RunContainer (ctx, " your/app" , testcontainer.RequestContainer {
71
- ExportedPort : []string {
72
- " 8081/tpc" ,
73
- },
74
- Env : map [string ]string {
75
- " REDIS_HOST" : fmt.Sprintf (" http://%s :6379" , redisIP),
76
- },
77
- })
78
- if err != nil {
79
- t.Error (err)
80
- }
81
- defer appC.Terminate (ctx)
73
+ appC , err := testcontainer.RunContainer (ctx, " your/app" , testcontainer.RequestContainer {
74
+ ExportedPort: []string {
75
+ " 8081/tpc" ,
76
+ },
77
+ Env: map [string ]string {
78
+ " REDIS_HOST" : fmt.Sprintf (" http://%s :6379" , redisIP),
79
+ },
80
+ })
81
+ if err != nil {
82
+ t.Error (err)
83
+ }
84
+ defer appC.Terminate (ctx, t )
82
85
83
- // your assertions
86
+ // your assertions
87
+ }
84
88
```
0 commit comments