Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Onpar ¶
type Onpar[T, U any] struct { // contains filtered or unexported fields }
Onpar stores the state of the specs and groups
func BeforeEach ¶
BeforeEach creates a new child Onpar suite with the requested function as the setup function for all specs. It requires a parent Onpar.
The top level Onpar *must* have been constructed with New, otherwise the suite will not run.
BeforeEach should be called only once for each level (i.e. each group). It will panic if it detects that it is overwriting another BeforeEach call for a given level.
func New ¶
New creates a new Onpar suite. The top-level onpar suite must be constructed with this. Think `context.Background()`.
It's normal to construct the top-level suite with a BeforeEach by doing the following:
o := BeforeEach(New(t), setupFn)
func (*Onpar[T, U]) AfterEach ¶
func (o *Onpar[T, U]) AfterEach(f func(U))
AfterEach is used to cleanup anything from the specs or BeforeEaches. AfterEach may only be called once for each *Onpar value constructed.
func (*Onpar[T, U]) Group ¶
Group is used to gather and categorize specs. Inside of each group, a new child *Onpar may be constructed using BeforeEach.
func (*Onpar[T, U]) Run ¶
func (o *Onpar[T, U]) Run()
Run runs all of o's tests. Typically this will be called in a `defer` immediately after o is defined:
o := onpar.BeforeEach(onpar.New(t), setupFn) defer o.Run()
func (*Onpar[T, U]) SerialSpec ¶
SerialSpec is a test that runs synchronously (i.e. onpar will not call `t.Parallel`). While onpar is primarily a parallel testing suite, we recognize that sometimes a test just can't be run in parallel. When that is the case, use SerialSpec.
type Table ¶ added in v0.3.2
type Table[T, U, V any] struct { // contains filtered or unexported fields }
Table is an entry to be used in table tests.
func TableSpec ¶ added in v0.3.2
TableSpec returns a Table type which may be used to declare table tests. The spec argument is the test that will be run for each entry in the table.
This is effectively syntactic sugar for looping over table tests and calling `parent.Spec` for each entry in the table.
func (Table[T, U, V]) Entry ¶ added in v0.3.2
Entry adds an entry to t using entry as the value for this table entry.
Example ¶
package main import ( "testing" "github.com/poy/onpar" ) func main() { var t *testing.T o := onpar.New(t) defer o.Run() type table struct { input string expectedOutput string } f := func(in string) string { return in + "world" } onpar.TableSpec(o, func(t *testing.T, tt table) { output := f(tt.input) if output != tt.expectedOutput { t.Fatalf("expected %v to produce %v; got %v", tt.input, tt.expectedOutput, output) } }). Entry("simple output", table{"hello", "helloworld"}). Entry("with a space", table{"hello ", "hello world"}). Entry("and a comma", table{"hello, ", "hello, world"}) }
Output:
func (Table[T, U, V]) FnEntry ¶ added in v0.3.2
FnEntry adds an entry to t that calls setup in order to get its entry value. The value from the BeforeEach will be passed to setup, and then both values will be passed to the table spec.
Example ¶
package main import ( "bytes" "testing" "github.com/poy/onpar" ) func main() { var t *testing.T o := onpar.New(t) defer o.Run() type table struct { input string expectedOutput string } f := func(in string) string { return in + "world" } onpar.TableSpec(o, func(t *testing.T, tt table) { output := f(tt.input) if output != tt.expectedOutput { t.Fatalf("expected %v to produce %v; got %v", tt.input, tt.expectedOutput, output) } }). FnEntry("simple output", func(t *testing.T) table { var buf bytes.Buffer if _, err := buf.WriteString("hello"); err != nil { t.Fatalf("expected buffer write to succeed; got %v", err) } return table{input: buf.String(), expectedOutput: "helloworld"} }) }
Output: