In go language , is it possible to show both the pass testcase and failed testcase in the console.
Suppose , I have a file test.go , which has 4 testcases , out of which 2 have passed and 2 have failed.
When we use t.Errorf(), function and then command "go test", then only the failed testcases with description is displayed.
So , Is there a way to show number of testcases passed and failed?
go test -v
Use go test help for a list of available flags.
http://golang.org/cmd/go/#hdr-Description_of_testing_flags
I use grep for it:
make test | grep FAIL
Related
This question already has answers here:
How to run test cases in a specified file?
(8 answers)
Closed 10 months ago.
I have a test suite for a Go package that implements a dozen tests. Sometimes, one of the tests in the suite fails and I'd like to re-run that test individually to save time in debugging process. Is this possible or do I have to write a separate file for this every time?
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.
In case someone that is using Ginkgo BDD framework for Go will have the same problem, this could be achieved in that framework by marking test spec as focused (see docs), by prepending F before It, Context or Describefunctions.
So, if you have spec like:
It("should be idempotent", func() {
You rewrite it as:
FIt("should be idempotent", func() {
And it will run exactly that one spec:
[Fail] testing Migrate setCurrentDbVersion [It] should be idempotent
...
Ran 1 of 5 Specs in 0.003 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 4 Skipped
Given a test:
func Test_myTest() {
//...
}
Run only that test with:
go test -run Test_myTest path/to/pkg/mypackage
Simple and reliable:
go test -run TestMyFunction ./...
More on ./... :
https://stackoverflow.com/a/28031651/5726621
Say your test suite is structured as following:
type MyTestSuite struct {
suite.Suite
}
func TestMyTestSuite(t *testing.T) {
suite.Run(t, new(MyTestSuite))
}
func (s *MyTestSuite) TestMethodA() {
}
To run a specific test of test suite in go, you need to use: -testify.m.
go test -v <package> -run ^TestMyTestSuite$ -testify.m TestMethodA
More simply, if the method name is unique to a package, you can always run this
go test -v <package> -testify.m TestMethodA
go test -v <package> -run <TestFunction>
In my go program, the main method does:
port := flag.Int("port", 8080, "Port number to start service on")
flag.Parse()
I have a dummy test that looks as:
func TestName(t *testing.T) {
fmt.Println("Hello there")
}
when I run my tests (from the goland or the command line) I got the following error stuck:
/usr/local/go/bin/go tool test2json -t /private/var/folders/7v/p2t5phhn6cn9hqwjnn_p95_80000gn/T/___TestName_in_github_tools....test -test.v -test.paniconexit0 -test.run ^\QTestName\E$
flag provided but not defined: -test.v
Usage of /private/var/folders/7v/p2t5phhn6cn9hqwjnn_p95_80000gn/T/___TestName_in_github_tools.....test:
-port int
Port number to start service on (default 8080)
When I remove the lines of the flag in the main, the test executes normally
Any idea on how to fix this, please?
Thanks in advance
When you run go test, go actually compiles your code into an executable, and executes it.
If you add options to go test -- for example : go test -v -- these options are actually passed to the test executable, prefixed with test -- so -v is turned into -test.v.
(this is a reason why several comments ask for the exact command line you use to run your tests : since the error is about -test.v, there probably is something that adds -v to some go test ... invocation)
It looks like flag.Parse() is trying to parse some arguments which are actually intended for your test executable, not for your code.
This is probably because it is called too early, before the test executable has had a chance to alter the os.Args slice to remove some specific flags.
Check what triggers a call to flag.Parse() : if it is executed from an init() block, this would count as "too early".
The behavior of go test options is documented in go help testflag :
Each of these flags is also recognized with an optional 'test.' prefix,
as in -test.v. When invoking the generated test binary (the result of
'go test -c') directly, however, the prefix is mandatory.
The 'go test' command rewrites or removes recognized flags,
as appropriate, both before and after the optional package list,
before invoking the test binary.
For instance, the command
go test -v -myflag testdata -cpuprofile=prof.out -x
will compile the test binary and then run it as
pkg.test -test.v -myflag testdata -test.cpuprofile=prof.out
This question already has answers here:
How to run test cases in a specified file?
(8 answers)
Closed 10 months ago.
I have a test suite for a Go package that implements a dozen tests. Sometimes, one of the tests in the suite fails and I'd like to re-run that test individually to save time in debugging process. Is this possible or do I have to write a separate file for this every time?
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.
In case someone that is using Ginkgo BDD framework for Go will have the same problem, this could be achieved in that framework by marking test spec as focused (see docs), by prepending F before It, Context or Describefunctions.
So, if you have spec like:
It("should be idempotent", func() {
You rewrite it as:
FIt("should be idempotent", func() {
And it will run exactly that one spec:
[Fail] testing Migrate setCurrentDbVersion [It] should be idempotent
...
Ran 1 of 5 Specs in 0.003 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 4 Skipped
Given a test:
func Test_myTest() {
//...
}
Run only that test with:
go test -run Test_myTest path/to/pkg/mypackage
Simple and reliable:
go test -run TestMyFunction ./...
More on ./... :
https://stackoverflow.com/a/28031651/5726621
Say your test suite is structured as following:
type MyTestSuite struct {
suite.Suite
}
func TestMyTestSuite(t *testing.T) {
suite.Run(t, new(MyTestSuite))
}
func (s *MyTestSuite) TestMethodA() {
}
To run a specific test of test suite in go, you need to use: -testify.m.
go test -v <package> -run ^TestMyTestSuite$ -testify.m TestMethodA
More simply, if the method name is unique to a package, you can always run this
go test -v <package> -testify.m TestMethodA
go test -v <package> -run <TestFunction>
How can I run a specific thread group in a Test Plan from the command line? I have a Test Plan (project file) that contains two "thread groups": one for crawling a site and another for calling specific urls with parameters. From the command line I execute with Maven, like so:
mvn.bat -Dnamescsv=src/test/resources/RandomLastNames.csv
-Ddomainhost=stgweb.domain.com -Dcrawlerthreads=2 -Dcrawlerloopcount=10 -Dsearchthreads=5 -Dsearchloopcount=5 -Dresultscsv=JmeterResults.csv clean test verify
I want to pass an argument to run only one of the two "thread group" in that project file. Can you do that with JMeter? I don't want to use an IF controller unless I have to because it feels like a "hack". I know that SoapUI lets you do this with the '-s' option.
I asked this question on the JMeter forum also.
In our tests we use the while controller. It doesn't look like a hack to me and works well. You can turn thread groups on and off easily with the JMeter properties. Note you can't change its status when the test is already running though.
Add While controller - ${__P(threadActive)}
Set JMeter property on JMeter load ( -JthreadActive = true )
Run test
Please note ${__P(threadActive)} equates to ${__P(threadActive)} == true, anything other than true will result in that thread group not running
In my Protractor test script, I use the usual notation:
describe("mytest") {
...
it(" should do this") {
...
it(" should do that") {
I would like to be able to see what test and what part of each is currently running when I run them. Is there any option I can use to output test descriptions to the console?
There is a reporter that should do what you are looking for. Take a look at https://www.npmjs.com/package/jasmine-spec-reporter and https://github.com/bcaudan/jasmine-spec-reporter/tree/master/examples/protractor
You can use the --verbose option to print more information about your tests, but it will not tell you which test is currently being run.
I suggest you to create an issue if you want that feature. https://github.com/angular/protractor/issues/new
$ ./node_modules/protractor/bin/protractor protractor-config.js --verbose
------------------------------------
PID: 7985 (capability: chrome #1)
------------------------------------
Using the selenium server at http://localhost:4444/wd/hub
angularjs homepage
should greet the named user
todo list
should list todos
should add a todo
Finished in 5.915 seconds
3 tests, 5 assertions, 0 failures
since protractor runs in node you can use console.log as you normally would in javascript, for more console fun see the node docs
I like to place any logs after functionality so that I know its been completed, so wrapping it inside of a .then() function seemed to work best for me
example:
element(elementToFind).click().then(function(){
console.log("clicked element");
continue();
});
If you need to know the name of the spec currently rinning you could use: jasmine.getEnv().currentSpec.description
and log it
console.log('\nTest spec: ' + __filename + '\n');
to log the test but no idea how to log the it being executed