Skip to content

Commit

Permalink
docs: Remove gocheck related section
Browse files Browse the repository at this point in the history
Signed-off-by: Tam Mach <tam.mach@cilium.io>
  • Loading branch information
sayboras committed May 14, 2024
1 parent a7d4e63 commit dc33966
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 137 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ linters-settings:
recommendations:
- testing
- github.com/stretchr/testify/assert
reason: "gocheck has been deprecated, see https://docs.cilium.io/en/latest/contributing/testing/unit/#migrating-tests-off-of-gopkg-in-check-v1"
reason: "gocheck has been deprecated, see https://github.com/cilium/cilium/issues/28596"
- go.uber.org/multierr:
recommendations:
- errors
Expand Down
136 changes: 0 additions & 136 deletions Documentation/contributing/testing/unit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ Integration Testing
Cilium uses the standard `go test <https://golang.org/pkg/testing/>`__ framework.
All new tests must use `the standard test framework`_.

Historically, `gocheck <https://gopkg.in/check.v1>`__ has also been used.
Many tests are still written using ``gocheck``, which creates some
unfortunate consequences:

1. Adding a test helper requires writing two versions: one for ``testing.T``
and one for ``check.C``.

2. Executing ``gocheck`` tests requires non-standard flags.

For this reason ``gopkg.in/check.v1`` has switched to a fork called
``github.com/cilium/checkmate``, which removes most of the problematic bits.
You can migrate away from ``gocheck`` by following the procedure outlined
in :ref:`migrate-gocheck`.

.. _the standard test framework: https://github.com/cilium/cilium/issues/16860

.. _integration_testing_prerequisites:
Expand Down Expand Up @@ -119,125 +105,3 @@ run this in a terminal next to your editor:
$ watchtest pkg/policy
This shell script depends on the ``inotify-tools`` package on Linux.

.. _migrate-gocheck:

Migrating tests off of ``gopkg.in/check.v1``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The Cilium codebase has historically used ``gopkg.in/check.v1`` to write unit and
integration tests. Most of the functionality offered by that package is now
present in `testing`_. What ``gocheck`` calls checkers are now available in
``testify``, in either `assert`_ or `require`_ form.

* ``check.Equals``: ``require.EqualValues``

* ``check.HasLen``: ``require.Len``

* ``check.ErrorMatches``: ``require.ErrorContains`` (doesn't support regex)

* ``checker.Equals`` and ``checker.DeepEquals``: ``require.Equal``

It's best to replace ``check.C.Assert`` with ``require`` calls instead of ``assert`` because the latter doesn't stop test execution if the assertion fails.

``gopkg.in/check.v1`` has been replaced with ``github.com/cilium/checkmate``, which
means that ``check.C`` now implements `testing.TB <https://pkg.go.dev/testing#TB>`__
and can be passed to test helpers that take ``testing.TB``.

The end goal is to remove all uses of ``checkmate`` from the codebase.
To convert a ``gocheck`` test, use the following approach:

1. Replace ``SetUp`` fixtures with helpers that take ``testing.TB``.

2. Replace ``TearDown`` fixtures with calls to ``testing.TB.Cleanup()``,
possibly in the helper you added to replace ``SetUp``.

3. Replace calls to ``c.Assert`` with ``require`` equivalents.

4. Replace tests methods with a normal ``func TestHelloWorld(t *testing.T)``
function.

Let's take an example from the ``gocheck`` documentation and migrate it:

.. code-block:: go
package hello_test
import (
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) SetUpTest(c *C) {
// setup code
}
func (s *MySuite) TearDownTest(c *C) {
// cleanup code
}
func (s *MySuite) TestHelloWorld(c *C) {
c.Assert(42, Equals, "42")
}
type SomeOtherSuite struct{}
var _ = Suite(&SomeOtherSuite{})
func (s *SomeOtherSuite) TestTheRealAnswer(c *C) {
c.Assert("42", Equals, "42")
}
After applying the previous rules you should end up with something like this:

.. code-block:: go
package hello_test
import (
"testing"
. "github.com/cilium/checkmate"
"github.com/stretchr/testify/assert"
)
func Test(t *testing.T) { TestingT(t) }
func setupHelper(tb testing.TB) {
tb.Helper()
// setup code
tb.Cleanup(func() {
// cleanup code
})
}
func TestHelloWorld(t *testing.T) {
setupHelper(t)
require.Equal(t, 42, "42")
}
type SomeOtherSuite struct{}
var _ = Suite(&SomeOtherSuite{})
func (s *SomeOtherSuite) TestTheRealAnswer(c *C) {
require.Equal(c, "42", "42")
}
As you can see we didn't get round to converting all tests yet.
Since ``C`` now implements ``testing.TB`` we can call ``require.Equal`` from
``TestTheRealAnswer()`` without problems.

.. _assert: https://pkg.go.dev/github.com/stretchr/testify/assert
.. _require: https://pkg.go.dev/github.com/stretchr/testify/require
.. _testing: https://golang.org/pkg/testing/

0 comments on commit dc33966

Please sign in to comment.