Home > other >  Go not running some tests
Go not running some tests

Time:09-27

I'm new to Golang and I'm trying to run some tests from a golang app. Tests are running at a docker container with a Golang 1.12.

My problem is that some tests appear to be running correctly, but others do not.

Example:

I have a function that I wanted to fail on purpose.

func TestLol(t *testing.T) {
    assert.EqualValues(t, 1, 2)
    t.Fail()
}

When I execute the docker container with a "docker run ... go test -v ./..." it should run all tests and about this function on specific it should fail, but what happens that it doesn't fail. Golang just log a "ok" beside the test.

Then I tried to run only the folder with test file that should fail.

Log:

ok      github.com/eventials/csw-notifications/services 0.016s
2021/09/25 21:08:44 Command finished successfully.
Tests exited with status code: 0
Stopping csw-notifications_db_1     ... done
Stopping csw-notifications_broker_1 ... done
Going to remove csw-notifications_app_run_ed70597b5c20, csw-notifications_db_1, csw-notifications_broker_1
Removing csw-notifications_app_run_ed70597b5c20 ... done
Removing csw-notifications_db_1                 ... done
Removing csw-notifications_broker_1             ... done 

My question is why golang dosn't output any log with a FAIL message for this file in specific?

I think it's somewhat related to this question, but as it didn't received any answear I'm reposting it.

Why the tests are not running ? ( Golang ) - goapp test - bug?

EDIT: I'm editting to this question be more clear.

CodePudding user response:

You can try add -timeout If your test files contain FuncTest with samenames rename. You can try change output to go test -v -json ./...

    -timeout d
        If a test binary runs longer than duration d, panic.
        If d is 0, the timeout is disabled.
        The default is 10 minutes (10m).

CodePudding user response:

main.go

func Start(s ...string) {
  fmt.Println(s)
}

main_test.go

func TestStart(t *testing.T) {
    Start("rocket")

    if 0 == 1 {
        t.Log("Houston, we have a problem")
        t.Fail()
    }
}

func ExampleStart() {
    fmt.Println("Ground Control to Major Tom")
    // Output:
    // Ground Control to Major Tom
}

Change if condition to 0 == 0 to see Fail with somelogs.

  • Related