Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run test cases in a specified file?

Tags:

go

People also ask

How do I run a specific test file in go?

To run your tests in this mode, run go test in your project's root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command. If a package test passes, go test prints only the final 'ok' summary line.

How do I run a single spec TS file?

ts file have all its tests grouped in describe block like this: describe('SomeComponent', () => {...} You can easily run just this single block, by prefixing the describe function name with f : fdescribe('SomeComponent', () => {...}

How do I run a specific test?

Use the go test -run flag to run a specific test. The flag is documented in the testing flags section of the go tool documentation: -run regexp Run only those tests and examples matching the regular expression.


There are two ways. The easy one is to use the -run flag and provide a pattern matching names of the tests you want to run. Example:

go test packageName -run NameOfTest

See the docs for more info. Note that the -run flag may also run other tests if they contain the string NameOfTest, as the -run flag matches a regexp. So to ensure that only a test named exactly 'NameOfTest' is run, one has to use the regexp ^NameOfTest$:

go test -run "^NameOfTest$" 

The other way is to name the specific file, containing the tests you want to run:

go test -v foo_test.go

But there's a catch. This works well if:

  • foo.go is in package foo.
  • foo_test.go is in package foo_test and imports 'foo'.

If foo_test.go and foo.go are the same package (a common case) then you must name all other files required to build foo_test. In this example it would be:

go test foo_test.go foo.go

I'd recommend to use the -run pattern. Or, where/when possible, always run all package tests.


@zzzz's answer is mostly complete, but just to save others from having to dig through the referenced documentation you can run a single test in a package as follows:

go test packageName -run TestName

Note that you want to pass in the name of the test, not the file name where the test exists.

The -run flag actually accepts a regex so you could limit the test run to a class of tests. From the docs:

-run regexp
    Run only those tests and examples matching the regular
    expression.

When running a single test I usually do:

go test -run TestSomethingReallyCool ./folder1/folder2/ -v -count 1

-count 1 also ensures that the test is ran every time instead of being cached. Useful when you are testing against race conditions and have a test that fails only sometimes. In Go versions not using modules the same could be achieved by setting GOCACHE=off but this interacts poorly with Go modules.


go test -v ./<package_name> -run Test

Prevents caching of test results.

go test -count=1 ./<package_name> -run Test

alias testcases="sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /|/g'"

go test -v -run $(cat coordinator_test.go | testcases)