Proper package naming for testing with the Go language

前端 未结 4 1323
无人及你
无人及你 2020-12-02 04:08

I have seen several different test package naming strategies within Go and wanted to know what pros and cons of each are and which one I should use.

Strategy

4条回答
  •  长情又很酷
    2020-12-02 04:19

    One important notes I'd like to add about import . from Golang CodeReviewComments:

    The import . form can be useful in tests that, due to circular dependencies, cannot be made part of the package being tested:

    package foo_test
    
    import (
        "bar/testutil" // also imports "foo"
        . "foo"
    )
    

    In this case, the test file cannot be in package foo because it uses bar/testutil, which imports foo. So we use the 'import .' form to let the file pretend to be part of package foo even though it is not.

    Except for this one case, do not use import . in your programs. It makes the programs much harder to read because it is unclear whether a name like Quux is a top-level identifier in the current package or in an imported package.

提交回复
热议问题