Recommended Books: using test-driven development go the way of learning language

First, the book is free.

Second, the book is open source.

If you want to learn go and TDD (Test Driven Development), then this book is a good choice.

Personally experience in terms of learning the language currently go better material is https://gobyexample.com, go to learn the language by example, do more with less.

And this way of using test-driven development of language learning go content is more three-dimensional, very suitable for everyone language while learning go have some knowledge of test-driven development.

What is test-driven development

Great concept, in simple terms, is a mental test-driven development, use cases to write is to write fulfill the spirit of the code.

Test-driven development actually works like this

  1. Unit to write test cases, the absence of a sensing function or functions
  2. Test execution unit, if it fails, modifying realized until test by
  3. Add more test cases
  4. Repeat steps 2 and reconstruction until all test functions are run by Example and with

The book's examples

You can have a perceptual awareness of the steps above by way of example in the book.

For example, implement a function addition, we can first write test cases to test this function, the name of this function is Add (int, int), this is Step 1

package integers

import "testing"

func TestAdder(t *testing.T) {
    sum := Add(2, 2)
    expected := 4

    if sum != expected {
        t.Errorf("expected '%d' but got '%d'", expected, sum)
    }
}

Then run the use case, then naturally fail because the Add () function we did not realize. Error as follows:

./adder_test.go:6:9: undefined: Add

Then implement Add () function, so that patients can use, there is a rule, is not to over-achieve.

Only a minimal amount of code written to run tests and check the output fail the test.

For example, you will see this is our implementation of

package integers

func Add(x, y int) int {
    return 0
}

This allows us to compile the code through, so that you can see in the end is what the use cases out what the problem

adder_test.go:10: expected '4' but got '0'

The following write enough code to make it through.

func Add(x, y int) int {
    return 4
}

Uh! ! ! ! What are you kidding me? Is this okay?

This really can.

Obviously, now the test case is passed, we Add () method also works well, which is to meet the demand, is not it?

Well, here it is interesting. Add a test case

func TestAdder(t *testing.T) {
    sum := Add(2, 2)
    expected := 4

    sum1 := Add(3, 5)
    expected1 := 8

    if sum != expected {
        t.Errorf("expected '%d' but got '%d'", expected, sum)
    }

    if sum1 != expected1 {
        t.Errorf("expected '%d' but got '%d'", expected, sum)
    }
}

Will run error, so we simply return to achieve 4 is wrong, because not flexible enough, not able to cover most of the scenes.

Then reconstruct it.

func Add(x, y int) int {
    return x + y
}

Now all use cases can be passed.

Then the realization Add () function is also almost the same.

How to learn

The code on the books in order to copy it again and running.

Especially the reconstruction part, sometimes seemed too much trouble, but the trouble is often the essence of a place.

Project Address: https://github.com/quii/learn-go-with-tests

Chinese Version: https://studygolang.gitbook.io/learn-go-with-tests/

Guess you like

Origin www.cnblogs.com/nbkhic/p/12610561.html