onsi / ginkgo

A Modern Testing Framework for Go
http://onsi.github.io/ginkgo/
MIT License
8.38k stars 660 forks source link

Running ginkgo with --focus for 'when' function #1486

Closed iamralch closed 2 days ago

iamralch commented 3 days ago

Presently, ginkgo is not consistent when you want to run tests that are using when instead of context.

Let's have the following suite:

package example_api_test

import (
    . "github.com/onsi/ginkgo/v2"
    . "github.com/onsi/gomega"
)

var _ = Describe("Example", func() {
    Context("when the numbers are positive", func() {
        It("returns the sum", func() {
            Expect(10 + 15).To(Equal(25))
        })
    })

    When("the numbers are negative", func() {
        It("returns the sum", func() {
            Expect(-10 + 15).To(Equal(5))
        })
    })
})

If you want to run the first test, you should run the following command:

ginkgo --focus '\bExample when the numbers are positive\b'

If you want to run the second test, you should run:

ginkgo --focus '\bExample when the numbers are negative\b'

The second test requires when prefix when you create the regexp. It's not obvious and consistent with the first call.

https://github.com/nvim-contrib/nvim-ginkgo/issues/3

onsi commented 3 days ago

Indeed it is not obvious, but it has been the behavior for several years and changing it may break existing user's scripts. I'm not sure what the best course of action is.

onsi commented 3 days ago

perhaps for your usecase (it looks like you are building developer tooling) it might be easier to use --focus-file and pass an explicit file and line number?

iamralch commented 2 days ago

@onsi appreciate your quick response. I think I have a clue on how to overcome that behaviour. I will close the issue for now.