@@ -31,7 +31,7 @@ func TestNginxLatestReturn(t *testing.T) {
3131 if err != nil {
3232 t.Error (err)
3333 }
34- defer nginxC.Terminate (ctx)
34+ defer nginxC.Terminate (ctx, t )
3535 ip , err := nginxC.GetIPAddress (ctx)
3636 if err != nil {
3737 t.Error (err)
@@ -46,39 +46,43 @@ This is a simple example, you can create one container in my case using the
4646` nginx ` image. You can get its IP ` ip, err := nginxC.GetIPAddress(ctx) ` and you
4747can use it to make a GET: ` resp, err := http.Get(fmt.Sprintf("http://%s", ip)) `
4848
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.
5052
5153You can build more complex flow using envvar to configure the containers. Let's
5254suppose you are testing an application that requites redis:
5355
5456``` 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+ }
6972
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 )
8285
83- // your assertions
86+ // your assertions
87+ }
8488```
0 commit comments