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
The `httptest.NewServer` function creates and starts an HTTP server on a random unused port. You need to provide an `http.Handler` implementation to process the request. Since this is a server, you must close it when the test completes. The server instance has its URL specified in the `URL` field of the server instance and a preconfigured `http.Client` for communicating with the test server.
1575
+
1576
+
## Integration Tests and Build Tags
1577
+
1578
+
The Go compiler provides _build tags_ to control when code is compiled. Build tags are specified on the first line of a file with a magic comment that starts with `// +build`. The original intent for build tags was to allow different code to be compiled on different platforms, but they are also useful for splitting tests into groups. Tests in files without build tags run all the time. These are the unit tests that don’t have dependencies on external resources. Tests in files with a build tag are only run when the supporting resources are available.
1579
+
1580
+
To run our integration test alongside the other tests we’ve written, use:
1581
+
1582
+
```sh-session
1583
+
$ go test -tags integration -v ./...
1584
+
```
1585
+
1586
+
---
1587
+
1588
+
> Using the -short flag
1589
+
1590
+
Another option is to use go test with the -short flag. If you want to skip over tests that take a long time, label your slow tests by placing the the following code at the start of the test function:
1591
+
1592
+
```go
1593
+
if testing.Short() {
1594
+
t.Skip("skipping test in short mode.")
1595
+
}
1596
+
```
1597
+
1598
+
When you want to run only short tests, pass the `-short` flag to go test.
1599
+
1600
+
There are a few problems with the -short flag. If you use it, there are only two levels of testing: short tests and all tests. By using build tags, you can group your integration tests, specifying which service they need in order to run. Another argument against using the -short flag to indicate integration tests is philosophical. Build tags indicate a dependency, while the -short flag is only meant to indicate that you don’t want to run tests that take a long time. Those are different concepts.
1601
+
1602
+
## Finding Concurrency Problems with the Race Checker
1603
+
1604
+
use the flag `-race` with `go test` to enable it:
1605
+
1606
+
```sh-session
1607
+
$ go test -race
1608
+
==================
1609
+
WARNING: DATA RACE
1610
+
Read at 0x00c000128070 by goroutine 10:
1611
+
test_examples/race.getCounter.func1()
1612
+
test_examples/race/race.go:12 +0x45
1613
+
1614
+
Previous write at 0x00c000128070 by goroutine 8:
1615
+
test_examples/race.getCounter.func1()
1616
+
test_examples/race/race.go:12 +0x5b
1617
+
```
1618
+
1619
+
You can also use the `-race flag` when you build your programs. This creates a binary that includes the race checker and that reports any races it finds to the console. This allows you to find data races in code that doesn’t have tests.
1620
+
1621
+
A binary with `-race` enabled runs approximately ten times slower than a normal binary. That isn’t a problem for test suites that take a second to run, but for large test suites that take several minutes, a 10x slowdown reduces productivity.
0 commit comments