ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - pavlo/gosuite: Test suites support for standard Go1.7 "testing" by leveraging Subtests feature
Test suites support for standard Go1.7 "testing" by leveraging Subtests feature - pavlo/gosuite
Visit Site

GitHub - pavlo/gosuite: Test suites support for standard Go1.7 "testing" by leveraging Subtests feature

GitHub - pavlo/gosuite: Test suites support for standard Go1.7 "testing" by leveraging Subtests feature

Go Suite

Build Status codecov

The support for test suites for Golang 1.7 and later.

Golang 1.7 featured Subtests that allowed you to group tests in order to share common setup and teardown logic. While that was a great addition to the testing package, it was a bit clunky syntactically. The GoSuite package leverages Golang's 1.7 Subtests feature, defines a simple TestSuite interface and runs test cases inside of them keeping setup/teardown logic for the whole suite and for single cases in place.

Quick Start

To start with, create a struct with the four methods implemented:

type MyTestSuite struct {
    // DB connection
    // etc
}

// SetUpSuite is called once before the very first test in suite runs
func (s *MyTestSuite) SetUpSuite() {
}

// TearDownSuite is called once after thevery last test in suite runs
func (s *MyTestSuite) TearDownSuite() {
}

// SetUp is called before each test method
func (s *MyTestSuite) SetUp() {
}

// TearDown is called after each test method
func (s *MyTestSuite) TearDown() {
}

Then add one or more test methods to it, prefixing them with Test prefix:

func (s *MyTestSuite) TestMyFirstTestCase(t *testing.T) {
    if !someJob {
        t.Fail("Unexpected failure!")
    }
}

Almost done! The only piece that remains is to run the suite! You do this by calling the Run method. Note, the enclosing TestIt method is a normal testing method you usually write in Go, nothing fancy at all!

func TestIt(t *testing.T) {
	Run(t, &MyTestSuite{})
}

Installation

To install Go Suite, use go get:

go get github.com/pavlo/gosuite

The import the pavlo/gosuite package into your code like this:

package yours

import (
  "testing"
  "github.com/pavlo/gosuite"
)

...

Complete Example

The complete example is shown to help you to see the whole thing on the same page. Note, it leverages the Is package for assertions... the package is great though indeed it is not required to use with Go Suite. The example however demonstrates a slick technique making the assertion methods available on the suite itself!


import (
  "testing"
  "github.com/pavlo/gosuite"
)

type Suite struct {
	*is.Is
	setUpSuiteCalledTimes    int
	tearDownSuiteCalledTimes int
	setUpCalledTimes         int
	tearDownUpCalledTimes    int
}

func (s *Suite) SetUpSuite() {
	s.setUpSuiteCalledTimes++
}

func (s *Suite) TearDownSuite() {
	s.tearDownSuiteCalledTimes++
}

func (s *Suite) SetUp() {
	s.setUpCalledTimes++
}

func (s *Suite) TearDown() {
	s.tearDownUpCalledTimes++
}

func TestIt(t *testing.T) {
    s := &Suite{Is: is.New(s.t)}
	gosuite.Run(t, s)
	
	s.Equal(1, s.setUpSuiteCalledTimes)
	s.Equal(1, s.tearDownSuiteCalledTimes)
	s.Equal(2, s.setUpCalledTimes)
	s.Equal(2, s.tearDownUpCalledTimes)
}

func (s *Suite) TestFirstTestMethod(t *testing.T) {
	s.Equal(1, s.setUpSuiteCalledTimes)
	s.Equal(0, s.tearDownSuiteCalledTimes)
	s.Equal(1, s.setUpCalledTimes)
	s.Equal(0, s.tearDownUpCalledTimes)
}

func (s *Suite) TestSecondTestMethod(t *testing.T) {
	s.Equal(1, s.setUpSuiteCalledTimes)
	s.Equal(0, s.tearDownSuiteCalledTimes)
	s.Equal(2, s.setUpCalledTimes)
	s.Equal(1, s.tearDownUpCalledTimes)
}

Running it with go test -v would emit this:

> go test -v

=== RUN   TestIt
=== RUN   TestIt/TestFirstTestMethod
=== RUN   TestIt/TestSecondTestMethod
--- PASS: TestIt (0.00s)
    --- PASS: TestIt/TestFirstTestMethod (0.00s)
    --- PASS: TestIt/TestSecondTestMethod (0.00s)
PASS
ok  	github.com/pavlo/gosuite	0.009s
Success: Tests passed.

License

Go Suite is released under the MIT License.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory