testing


Node.js

  1. const test = require('tape')
  2. test(t => {
  3. const tt = [
  4. {a:1, b:1, ret:2},
  5. {a:2, b:3, ret:5},
  6. {a:5, b:5, ret:10}
  7. ]
  8. t.plan(tt.length)
  9. tt.forEach(tt => {
  10. t.equal(sum(tt.a, tt.b), tt.ret)
  11. })
  12. })
  13. function sum(a, b) {
  14. return a + b
  15. }

Output

  1. $ node examples/example_test.js
  2. TAP version 13
  3. # (anonymous)
  4. ok 1 should be equal
  5. ok 2 should be equal
  6. ok 3 should be equal
  7. 1..3
  8. # tests 3
  9. # pass 3
  10. # ok

Go

  1. package example
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestSum(t *testing.T) {
  7. for _, tt := range []struct {
  8. a int
  9. b int
  10. ret int
  11. }{
  12. {1, 1, 2},
  13. {2, 3, 5},
  14. {5, 5, 10},
  15. } {
  16. t.Run(fmt.Sprintf("(%v + %v)", tt.a, tt.b), func(t *testing.T) {
  17. ret := sum(tt.a, tt.b)
  18. if ret != tt.ret {
  19. t.Errorf("want %v, got %v", tt.ret, ret)
  20. }
  21. })
  22. }
  23. }
  24. func sum(a, b int) int {
  25. return a + b
  26. }

Output

  1. $ go test -v examples/example_test.go
  2. === RUN TestSum
  3. === RUN TestSum/(1_+_1)
  4. === RUN TestSum/(2_+_3)
  5. === RUN TestSum/(5_+_5)
  6. --- PASS: TestSum (0.00s)
  7. --- PASS: TestSum/(1_+_1) (0.00s)
  8. --- PASS: TestSum/(2_+_3) (0.00s)
  9. --- PASS: TestSum/(5_+_5) (0.00s)
  10. PASS
  11. ok command-line-arguments 0.008s