conveyz

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 15 Imported by: 0

README

gopherz

ezpkg.io/conveyz

PkgGoDev GitHub License version

Package conveyz extends the package convey with additional functionality and make it work with gomega. See the original blog post.

Installation

go get -u ezpkg.io/conveyz@v0.1.2

Features

  • Convey functions to group tests. SConvey to skip and FConvey to focus on a test.
  • Tests can be nested, and will be executed in nested order. In the example below, it will print:
[0] → [1] → [1.1]
[0] → [1] → [1.2]
[0] → [2] → [2.1]
[0] → [2] → [2.2]
Differences to the original package
  • This package ezpkg.io/conveyz uses gomega for assertions. To me, gomega is more powerful and easier to use.
  • With this package, we only need to set FConvey at a single block level. The original package requires to set FocusConvey at every nested level.
  • FConvey and SConvey will make go test fail. This is to avoid accidentally skipping tests. To skip tests but not make go test fail, use SkipConveyAsTODO.
  • Output looks better with more colors. Stacktrace is nicer.

Examples

See conveyz/examples/conveyz_test.go or stringz/stringz_test.go for more examples.

import (
	"fmt"
	"testing"

	. "github.com/onsi/gomega"
	. "ezpkg.io/conveyz"
)

func Test(t *testing.T) {
	Ω := GomegaExpect // 👈 make Ω as alias for GomegaExpect
	Convey("Start", t, func() {
		s := "[0]"
		defer func() { fmt.Printf("\n%s\n", s) }()

		add := func(part string) {
			s = AppendStr(s, part)
		}

		Convey("Test 1:", func() {
			add(" → [1]")
			Ω(s).To(Equal("[0] → [1]"))

			Convey("Test 1.1:", func() {
				add(" → [1.1]")
				Ω(s).To(Equal("[0] → [1] → [1.1]"))
			})
			Convey("Test 1.2:", func() {
				add(" → [1.2]")
				Ω(s).To(Equal("[0] → [1] → [1.2]"))
			})
		})
        // 👇change to FConvey to focus on this block and all children
        // 👇change to SConvey to skip the block
		// 👇change to SkipConveyAsTODO to mark as TODO
		Convey("Test 2:", func() {
			add(" → [2]")
			Ω(s).To(Equal("[0] → [2]"))
			
			Convey("Test 2.1:", func() {
				add(" → [2.1]")
				Ω(s).To(Equal("[0] → [2] → [2.1]"))
			})
			Convey("Test 2.2:", func() {
				add(" → [2.2]")
				Ω(s).To(Equal("[0] → [2] → [2.2]"))
			})
		})
		SkipConvey("failure message", func() {
			// 👆change SkipConvey to Convey to see failure messages
			// 👆change SkipConvey to SkipConveyAsTODO to mark as TODO

			Convey("fail", func() {
				//  Expected
				//      <string>: [0] → [2]
				//  to equal
				//      <string>: this test will fail
				Ω(s).To(Equal("this test will fail"))
			})
			Convey("UNEXPECTED", func() {
				// UNEXPECTED: Refusing to compare <nil> to <nil>.
				//  Be explicit and use BeNil() instead.  This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.
				Ω(nil).To(Equal(nil))
			})
		})
	})
}

func AppendStr(s, part string) string {	return s + part }
func WillPanic() { panic("let's panic! 💥") }
func CallFunc(fn func()) { fn() }
The output will look like this:

About ezpkg.io

As I work on various Go projects, I often find myself creating utility functions, extending existing packages, or developing packages to solve specific problems. Moving from one project to another, I usually have to copy or rewrite these solutions. So I created this repository to have all these utilities and packages in one place. Hopefully, you'll find them useful as well.

For more information, see the main repository.

Author

Oliver Nguyen  github

Documentation

Overview

Package conveyz extends the package convey with additional functionality and make it work with gomega.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convey

func Convey(items ...any)

func FConvey

func FConvey(items ...any)

FConvey (alias of FocusConvey) runs the current scope and all child scopes, but skips all other scopes. It also makes the test fail.

func FocusConvey

func FocusConvey(items ...any)

FocusConvey runs the current scope and all child scopes, but skips all other scopes. It also makes the test fail.

func GomegaExpect

func GomegaExpect(actual any, extra ...any) gomega.Assertion

GomegaExpect is an adapter to make gomega work with goconvey.

Usage: Ω := GomegaExpect

func Reset

func Reset(action func())

Reset registers a cleanup function to run after each Convey() in the same scope.

func SConvey

func SConvey(items ...any)

SConvey (alias of SkipConvey) skips the current scope and all child scopes. It also makes the test fail.

func SkipConvey

func SkipConvey(items ...any)

SkipConvey skips the current scope and all child scopes. It also makes the test fail.

func SkipConveyAsTODO added in v0.1.0

func SkipConveyAsTODO(items ...any)

SkipConveyAsTODO is similar to SkipConvey but does not make the test fail.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL