"TestMain(m *testing.M)" runs as a test itself but is failing
已完成
I'm using a file main_test.go to run my tests. There are 4 tests total but my run tells me that 1 test failed and 4 passed. I can only assume that TestMain() is that fifth test that is failing.
I have the code snippet below. I ran the 4 tests separately (from a separate file and skip the TestMain()) and got 0 tests failed and 4 tests passed.
When I run all 4 tests from TestMain() I get 1 test failed and 4 tests passed.
If I remove everything and have TestMain() do nothing (which is what the code snippet is showing), I still get "Tests failed: 1, passed: 0" but my console has "Process finished with exit code 0."
Am I supposed to mark the m variable with a pass/fail value?
func TestMain(m *testing.M) {
//retCode := m.Run() <=== originally ran 4 passing tests returning retCode = 0
//retCode := 0 <=== tried setting value when I commented out the above line
//os.Exit(retCode) <=== exiting with 0 still says this test fails
}
请先登录再写评论。
Hello Regina,
The basic implementation of the TestMain function works for me without any problems, all three tests in the file (in different files also) are run and only they are displayed.
Which version of Go and GoLand are you using? Could you attach a project repository?
I look forward to your reply.
I'm using GoLand 2020.1.4 that I upgraded to today. Now the test doesn't run through init(), setUp(), etc. Looks like the test can't load anymore with the update.
I changed the name of my testcase from main_test.go to e2e_test.go because I know Go is so picky about naming conventions.
I've narrowed the problem down. I was importing something and that import is causing me the grief. The code is company specific but I'll try to describe it. (I'm not sure you can help me since it is my company's code, but there may be some fundamental issue I'm just not seeing).
I have a project called sample. Inside sample is main.go and several packages. I'm importing it as
My test program is in another project. I'm not using anything in the services package right now (just to keep the test simple I'm only importing it) and I can't run my tests. I've attached a screen shot.
I'm wondering if the TestMain() can't load in e2e_test.go because it is importing a package that has a main.go in it. Plus, that package is in another project. The test run (bottom right-hand window) used to say that the process went through init(), setup(), and terminate() and then ended with an exit code of 0. Now with the Goland update, what you see in that window is the extend of what is run. I don't believe the test is actually initializing correctly.
BTW, I can still run the single tests just fine with this import statement. The import is only messing up when the e2e_test.go imports it.
Based on the documentation: "To write a new test suite, create a file whose name ends _test.go that contains the TestXxx functions as described here. Put the file in the same package as the one being tested.". It is the first point that you need to pay attention to so that the names of the packages match.
Regarding warnings in the Terminal: it does not apply to GoLand, but refers to the imported package, for more details, see the issue on GitHub: ""incompatible pointer types" warnings building the Go (golang) pulsar client".
From your screenshot, I can see the tests are running (4 tests out of 5 passed). What do you mean by saying they won't start? Does everything work fine if you run it from the Terminal and not from GoLand? How did you run tests earlier with init(), setUp() functions?
It will be great if you create some kind of test project that completely or partially repeats the one you are working on. Without it, it is complicated to analyze the situation and understand what is happening.
I look forward to hearing from you.
With the import in place, this works on the terminal line:
go test -v *.go
So I can run my tests but I’d like to run them through Goland's run/debug configurations and not just the terminal. I want my e2e_test.go to have my TestMain() function as well as all the code for connecting to the database in my init() and setting up my test objects in setup(). I also enjoy using the debugger so I will need to find a way that I can run this through a debug configuration and a run configuration.
My real problem is that I can't run “go test -v <path>/*.go” from the “run configuration.” What I'd like to do is SETUP 1 (see below). I’ve tried several different types of “run configurations” and I’ve listed them below with their results.
SETUP 1
Test Framework: gotest
Test Kind: File
Files: <path>/e2e/*.go
Error: File doesn't exist: <path> /e2e/*.go (I get this in the run configuration window even before saving it)
SETUP 2
Test Framework: gotest
Test Kind: Directory
Files: <path>/e2e (directory where all the files live)
Output: The problem I described above where the test doesn’t run correctly (i.e. doesn’t get to the init(), setup(), MainTest(), or teardown() functions)
SETUP 3
Test Framework: gotest
Test Kind: File
Files: <path>/e2e/e2e_test.go
Output: I run from the “run configuration” and it reaches init(), setup(), MainTest(), and teardown() but says there are no tests found to run
SETUP 4
Test Framework: gotest
Test Kind: package
Files: There is no path I can give. I can’t build a package because there are only test files in this directory.
Conclusion - I would really like to get SETUP 1 above to work. Is this possible?
I created a repository for your situation: go-tests-examples. It repeats the situation you described (the folder contains only tests and init(), MainTest(), setup(), teardown()). You need to create a new config and select test kind: directory or package.
Can you download and run it? In it, all the functions you described work correctly, and TestMain() is not considered a separate test.
What is the difference between my project and yours (you can add changes by making a PR)?
Can you tell me how you ran this by taking a screenshot of your run configuration?
Thanks Daniil. I've loaded your code and it does work. However, when I put my import statement in your code it breaks while building the test. This happens for both running in a "run config" and for running inside the terminal with "go test -v *.go"
So I can reproduce similar results with your code. My only difference is that I do not see the failure line (see below) with my code and I can run and get good results from the terminal with "go test -v *.go" with my code
I was able to capture the error lines with your code and it looks like the package I'm importing has an error in it. The pulsar line is just a warning but the last two lines are failures:
[more lines deleted]
# github.com/apache/pulsar/pulsar-client-go/pulsar
In file included from ../../../workspace/src/github.com/apache/pulsar/pulsar-client-go/pulsar/c_reader.go:23:
../../../workspace/src/github.com/apache/pulsar/pulsar-client-go/pulsar/c_go_pulsar.h:70:51: warning: incompatible pointer types passing 'void (pulsar_result, pulsar_message_t *, void *)' (aka 'void (pulsar_result, struct _pulsar_message *, void *)') to parameter of type 'pulsar_send_callback' (aka 'void (*)(pulsar_result, struct _pulsar_message_id *, void *)') [-Wincompatible-pointer-types]
/usr/local/include/pulsar/c/producer.h:81:68: note: passing argument to parameter 'callback' here
# github.com/company/company-framework/services
../../../workspace/src/github.com/company/company-framework/services/redis.go:44:24: not enough arguments in call to srv.Client.cmdable.Set
have (string, interface {}, time.Duration)
want (context.Context, string, interface {}, time.Duration)
../../../workspace/src/github.com/company/company-framework/services/redis.go:52:29: not enough arguments in call to srv.Client.cmdable.Get
have (string)
want (context.Context, string)
FAIL _/Users/reginamorris/sample/go-tests-examples/tests [build failed]
So I think my best bet at this point is to contact someone within the company familiar with that code and have them fix it. I'm not sure why my code runs and doesn't give me this failure message. My code runs up to the pulsar warning and just dies when ran through a configuration, but then works if run through the terminal. Crazy!
Thanks for all of your help. I really do appreciate it.
Regina
I'm attaching a screenshot of the configuration below.
As a suggestion from my side, I recommend checking the version of the current local dependency and the current one in the repository, perhaps they don't match, and the problem has already been fixed earlier. Indeed, contacting one of your colleagues would be the right decision.
By the way, if you need any further help, feel free to write to me.
It was a version mismatch. Thanks for the help. :-)
This was not a version mismatch. I think I have the correct question to ask but I don't have the answer. :-(
I have a package dbconnect. I have a directory with two files.
dbconnect.go
dbconnect_test.go
I'm running as a gotest framework and as a directory.
Through Goland, I do a "go build -v dbconnect" and then a "go test -v dbconnect." I had to video capture my screen so I could see where the error actually occurs because this happens too fast.
This is a screenshot of a screenshot but it shows the second right before the error occurs.
And then the error occurs while loading TestDbconnect. You can see that it claims (in red) that "Tests failed: 1 of 1 test." There is only 1 test in this package and it is TestDbconnect().
Then it actually runs my TestDbconnect(), it passes, and it is marked as passed: 1 of 2 tests":
This is the error a second later.
So I started thinking about the command line, which always works.
I cd into the directory and run "run test -v" and it works just fine.
I then type in "run test -v dbconnect" which (I think is how Goland is running it). I got this error:
$ go test -v dbconnect
can't load package: package dbconnect is not in GOROOT (/usr/local/go/src/dbconnect)
So am I supposed to build all packages and make sure then get put into GOROOT/src directory? Or am I supposed to change this GOROOT directory each time I build?
I changed the GOROOT directory and got this:
$ pwd
/Users/reginamorris/workspace/koddi-framework/test/dbconnect
$ export GOROOT=/Users/reginamorris/workspace/koddi-framework/test/dbconnect
$ echo $GOROOT
/Users/reginamorris/workspace/koddi-framework/test/dbconnect
$ go test -v dbconnect
can't load package: package dbconnect is not in GOROOT (/Users/reginamorris/workspace/koddi-framework/test/dbconnect/src/dbconnect)
So that doesn't work.
How do I create a package for my *_test.go files?
Hello,
As I noted in your new thread, when launched configuration on the GoLand directory, it executes the go test -v ./... command. go test -v is almost equivalent to go test ./ ... command. You don't need to provide the package name.
Here the problem is in the package you are importing. It cannot compile (also try compiling the imported package manually). To fix those errors/warnings, try installing libpulsar library via Homebrew.
Please write will it help or not.