Saturday 3 August 2019

Testing Go with Ginkgo


>ginkgo help
Ginkgo Version 1.8.0

ginkgo --
--------------------------------------------
Run the tests in the passed in (or the package in the current directory if left blank).
Any arguments after -- will be passed to the test.
Accepts the following flags:
-a Force rebuilding of packages that are already up-to-date.
-afterSuiteHook string
Run a command when a suite test run completes
-asmflags string
Arguments to pass on each go tool asm invocation.
-blockprofilerate int
Control the detail provided in goroutine blocking profiles by calling runtime.SetBlockProfileRate with the given value. (default 1)
-buildmode string
Build mode to use. See 'go help buildmode' for more.
-compiler string
Name of compiler to use, as in runtime.Compiler (gccgo or gc).
-compilers int
The number of concurrent compilations to run (0 will autodetect)
-cover
Run tests with coverage analysis, will generate coverage profiles with the package name in the current directory.
-covermode string
Set the mode for coverage analysis.
-coverpkg string
Run tests with coverage on the given external modules.
-coverprofile string
Write a coverage profile to the specified file after all tests have passed.
-cpuprofile string
Write a CPU profile to the specified file before exiting.
-debug
If set, ginkgo will emit node output to files when running in parallel.
-dryRun
If set, ginkgo will walk the test hierarchy without actually running anything. Best paired with -v.
-failFast
If set, ginkgo will stop running a test suite after a failure occurs.
-failOnPending
If set, ginkgo will mark the test suite as failed if any specs are pending.
-flakeAttempts int
Make up to this many attempts to run each spec. Please note that if any of the attempts succeed, the suite will not be failed. But any failures will still be recorded. (default 1)
-focus string
If set, ginkgo will only run specs that match this regular expression.
-gccgoflags string
Arguments to pass on each gccgo compiler/linker invocation.
-gcflags string
Arguments to pass on each go tool compile invocation.
-installsuffix string
A suffix to use in the name of the package installation directory.
-keepGoing
When true, failures from earlier test suites do not prevent later test suites from running
-ldflags string
Arguments to pass on each go tool link invocation.
-linkshared
Link against shared libraries previously created with -buildmode=shared.
-memprofile string
Write a memory profile to the specified file after all tests have passed.
-memprofilerate int
Enable more precise (and expensive) memory profiles by setting runtime.MemProfileRate.
-mod string
Go module control. See 'go help modules' for more.
-msan
Enable interoperation with memory sanitizer.
-n go test
Have go test print the commands but do not run them.
-noColor
If set, suppress color output in default reporter.
-nodes int
The number of parallel test nodes to run (default 1)
-noisyPendings
If set, default reporter will shout about pending tests. (default true)
-noisySkippings
If set, default reporter will shout about skipping tests. (default true)
-outputdir string
Place output files from profiling in the specified directory.
-p Run in parallel with auto-detected number of nodes
-pkgdir string
install and load all packages from the given dir instead of the usual locations.
-progress
If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter.
-r Find and run test suites under the current directory recursively.
-race
Run tests with race detection enabled.
-randomizeAllSpecs
If set, ginkgo will randomize all specs together. By default, ginkgo only randomizes the top level Describe, Context and When groups.
-randomizeSuites
When true, Ginkgo will randomize the order in which test suites run
-regexScansFilePath
If set, ginkgo regex matching also will look at the file path (code location).
-requireSuite
Fail if there are ginkgo tests in a directory but no test suite (missing RunSpecs)
-seed int
The seed used to randomize the spec suite. (default 1552989981)
-skip string
If set, ginkgo will only run specs that do not match this regular expression.
-skipMeasurements
If set, ginkgo will skip any measurement specs.
-skipPackage string
A comma-separated list of package names to be skipped. If any part of the package's path matches, that package is ignored.
-slowSpecThreshold float
(in seconds) Specs that take longer to run than this threshold are flagged as slow by the default reporter. (default 5)
-stream
stream parallel test output in real time: less coherent, but useful for debugging (default true)
-succinct
If set, default reporter prints out a very succinct report
-tags string
A list of build tags to consider satisfied during the build.
-timeout duration
Suite fails if it does not complete within the specified timeout (default 24h0m0s)
-toolexec string
a program to use to invoke toolchain programs like vet and asm.
-trace
If set, default reporter prints out the full stack trace when a failure occurs
-untilItFails
When true, Ginkgo will keep rerunning tests until a failure occurs
-v If set, default reporter print out all specs as they begin.
-vet string
Configure the invocation of 'go vet' to use the comma-separated list of vet checks. If list is 'off', 'go test' does not run 'go vet' at all.
-work
Print the name of the temporary work directory and do not delete it when exiting.
-x go test
Have go test print the commands.

ginkgo watch --
--------------------------------------------------
Watches the tests in the passed in and runs them when changes occur.
Any arguments after -- will be passed to the test.
Accepts all the flags that the ginkgo command accepts except for --keepGoing and --untilItFails

ginkgo build
-------------------------------
Build the passed in (or the package in the current directory if left blank).
Accepts the following flags:
-a Force rebuilding of packages that are already up-to-date.
-asmflags string
Arguments to pass on each go tool asm invocation.
-blockprofilerate int
Control the detail provided in goroutine blocking profiles by calling runtime.SetBlockProfileRate with the given value. (default 1)
-buildmode string
Build mode to use. See 'go help buildmode' for more.
-compiler string
Name of compiler to use, as in runtime.Compiler (gccgo or gc).
-cover
Run tests with coverage analysis, will generate coverage profiles with the package name in the current directory.
-covermode string
Set the mode for coverage analysis.
-coverpkg string
Run tests with coverage on the given external modules.
-coverprofile string
Write a coverage profile to the specified file after all tests have passed.
-cpuprofile string
Write a CPU profile to the specified file before exiting.
-gccgoflags string
Arguments to pass on each gccgo compiler/linker invocation.
-gcflags string
Arguments to pass on each go tool compile invocation.
-installsuffix string
A suffix to use in the name of the package installation directory.
-ldflags string
Arguments to pass on each go tool link invocation.
-linkshared
Link against shared libraries previously created with -buildmode=shared.
-memprofile string
Write a memory profile to the specified file after all tests have passed.
-memprofilerate int
Enable more precise (and expensive) memory profiles by setting runtime.MemProfileRate.
-mod string
Go module control. See 'go help modules' for more.
-msan
Enable interoperation with memory sanitizer.
-n go test
Have go test print the commands but do not run them.
-outputdir string
Place output files from profiling in the specified directory.
-pkgdir string
install and load all packages from the given dir instead of the usual locations.
-r Find and run test suites under the current directory recursively.
-race
Run tests with race detection enabled.
-requireSuite
Fail if there are ginkgo tests in a directory but no test suite (missing RunSpecs)
-skipPackage string
A comma-separated list of package names to be skipped. If any part of the package's path matches, that package is ignored.
-tags string
A list of build tags to consider satisfied during the build.
-toolexec string
a program to use to invoke toolchain programs like vet and asm.
-vet string
Configure the invocation of 'go vet' to use the comma-separated list of vet checks. If list is 'off', 'go test' does not run 'go vet' at all.
-work
Print the name of the temporary work directory and do not delete it when exiting.
-x go test
Have go test print the commands.

ginkgo bootstrap
------------------------
Bootstrap a test suite for the current package
Accepts the following flags:
-agouti
If set, bootstrap will generate a bootstrap file for writing Agouti tests
-internal
If set, generate will generate a test file that uses the regular package name
-nodot
If set, bootstrap will generate a bootstrap file that does not . import ginkgo and gomega
-template string
If specified, generate will use the contents of the file passed as the bootstrap template

ginkgo generate
-----------------------------
Generate a test file named filename_test.go
If the optional argument is omitted, a file named after the package in the current directory will be created.
Accepts the following flags:
-agouti
If set, generate will generate a test file for writing Agouti tests
-internal
If set, generate will generate a test file that uses the regular package name
-nodot
If set, generate will generate a test file that does not . import ginkgo and gomega

ginkgo nodot
------------
Update the nodot declarations in your test suite
Any missing declarations (from, say, a recently added matcher) will be added to your bootstrap file.
If you've renamed a declaration, that name will be honored and not overwritten.

ginkgo convert /path/to/package
-------------------------------
Convert the package at the passed in path from an XUnit-style test to a Ginkgo-style test

ginkgo unfocus (or ginkgo blur)
-------------------------------
Recursively unfocuses any focused tests under the current directory

ginkgo version
--------------
Print Ginkgo's version

ginkgo help
---------------------
Print usage information. If a command is passed in, print usage information just for that command.


---

To create test file for some package (example):

../github.com/BojanKomazec/go-demo/internal/pkg/stringdemo$ ginkgo bootstrap
Generating ginkgo test suite bootstrap for stringdemo in:
        stringdemo_suite_test.go

Generated file stringdemo_suite_test.go looks like this:

package stringdemo_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestStringdemo(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Stringdemo Suite")
}

This file has to have name in form *_test.go.

package name can be adjusted to match the name of the package which is under test (e.g. package stringdemo instead of stringdemo_test). This can be done during the bootstrap by passing -internal argument to it.

---

To run all tests across all packages in the project and also print the coverage % use:

ginkgo -r -v -cover

To achieve the same with native go test do the following:

go test ./... -v -cover


Panic in a goroutine crashes test suite

If running dlv test on tests written with Ginkgo framework:

API server listening at: 127.0.0.1:9379

Usage of C:\...\git.bk.com\example+project\internal\example_package\debug.test:
  -ginkgo.debug
    If set, ginkgo will emit node output to files when running in parallel.
  -ginkgo.dryRun
    If set, ginkgo will walk the test hierarchy without actually running anything.  Best paired with -v.
  -ginkgo.failFast
    If set, ginkgo will stop running a test suite after a failure occurs.
  -ginkgo.failOnPending
    If set, ginkgo will mark the test suite as failed if any specs are pending.
  -ginkgo.flakeAttempts int
    Make up to this many attempts to run each spec. Please note that if any of the attempts succeed, the suite will not be failed. But any failures will still be recorded. (default 1)
  -ginkgo.focus string
    If set, ginkgo will only run specs that match this regular expression.
  -ginkgo.noColor
    If set, suppress color output in default reporter.
  -ginkgo.noisyPendings
    If set, default reporter will shout about pending tests. (default true)
  -ginkgo.noisySkippings
    If set, default reporter will shout about skipping tests. (default true)
  -ginkgo.parallel.node int
    This worker node's (one-indexed) node number.  For running specs in parallel. (default 1)
  -ginkgo.parallel.streamhost string
    The address for the server that the running nodes should stream data to.
  -ginkgo.parallel.synchost string
    The address for the server that will synchronize the running nodes.
  -ginkgo.parallel.total int
    The total number of worker nodes.  For running specs in parallel. (default 1)
  -ginkgo.progress
    If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter.
  -ginkgo.randomizeAllSpecs
    If set, ginkgo will randomize all specs together.  By default, ginkgo only randomizes the top level Describe, Context and When groups.
  -ginkgo.regexScansFilePath
    If set, ginkgo regex matching also will look at the file path (code location).
  -ginkgo.seed int
    The seed used to randomize the spec suite. (default 1553106348)
  -ginkgo.skip string
    If set, ginkgo will only run specs that do not match this regular expression.
  -ginkgo.skipMeasurements
    If set, ginkgo will skip any measurement specs.
  -ginkgo.slowSpecThreshold float
    (in seconds) Specs that take longer to run than this threshold are flagged as slow by the default reporter. (default 5)
  -ginkgo.succinct
    If set, default reporter prints out a very succinct report
  -ginkgo.trace
    If set, default reporter prints out the full stack trace when a failure occurs
  -ginkgo.v
    If set, default reporter print out all specs as they begin.
  -test.bench regexp
    run only benchmarks matching regexp
  -test.benchmem
    print memory allocations for benchmarks
  -test.benchtime d
    run each benchmark for duration d (default 1s)
  -test.blockprofile file
    write a goroutine blocking profile to file
  -test.blockprofilerate rate
    set blocking profile rate (see runtime.SetBlockProfileRate) (default 1)
  -test.count n
    run tests and benchmarks n times (default 1)
  -test.coverprofile file
    write a coverage profile to file
  -test.cpu list
    comma-separated list of cpu counts to run each test with
  -test.cpuprofile file
    write a cpu profile to file
  -test.failfast
    do not start new tests after the first test failure
  -test.list regexp
    list tests, examples, and benchmarks matching regexp then exit
  -test.memprofile file
    write an allocation profile to file
  -test.memprofilerate rate
    set memory allocation profiling rate (see runtime.MemProfileRate)
  -test.mutexprofile string
    write a mutex contention profile to the named file after execution
  -test.mutexprofilefraction int
    if >= 0, calls runtime.SetMutexProfileFraction() (default 1)
  -test.outputdir dir
    write profiles to dir
  -test.parallel n
    run at most n tests in parallel (default 4)
  -test.run regexp
    run only tests and examples matching regexp
  -test.short
    run smaller test suite to save time
  -test.testlogfile file
    write test action log to file (for use only by cmd/go)
  -test.timeout d
    panic test binary after duration d (default 0, timeout disabled)
  -test.trace file
    write an execution trace to file
  -test.v
    verbose: print additional output

Current Directory in Unit Tests


If test file is in module/module_test.go, the current directory (denoted with "./") in unit tests in module_test.go will be set to module.


DescribeTable

ginkgo/table.go at master · onsi/ginkgo:
Under the hood, DescribeTable simply generates a new Ginkgo Describe
Each Entry is turned into an It within the Describe
It's important to understand that the Describes and Its are generated at evaluation time (i.e. when Ginkgo constructs the tree of tests and before the tests run).

The last paragraph explains why it is not good idea to use values that are results of calls on SUT as values passed to Entry. SUT might not be constructed or be in the proper state for those calls so we try to access SUT in evaluation time we might get errors like:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x80e29e]

In general, good TDD practice is to use hardcoded values as expected values, not those calculated from SUT as they might be wrong.


Use --failFast to stop executing tests as soon as one test fails:

$ ginkgo --failFast ./...

 

Resources:

 

1 comment:

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : (
atmmachinehackers1@gmail.com ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;
atmmachinehackers1@gmail.com