Testing within packages

274 views
Skip to first unread message

jor...@wastedintelligence.com

unread,
Apr 24, 2014, 7:48:48 PM4/24/14
to ginkgo-a...@googlegroups.com
Up to this point I've been putting all of my tests into a "tests" directory and testing packages externally. That being said, I'd like to be able to access package internals to simplify my tests; I don't want to have to examine state externally, since some of these checks would be somewhat exhaustive. I'm currently exporting functions that I'd rather not, just for the sake of testing.

I'd like to move my tests into the packages they're testing so that I can stop exporting internal functions. Doing that is relatively straight-forward, but maintaining the tie-in to my project-wide "suite test"/ginkgo setup such that "go test" works isn't obvious. Can someone shed some light on how I can have these tests scattered throughout my project, but still be able to run the entire suite?

Onsi Fakhouri

unread,
Apr 24, 2014, 8:56:51 PM4/24/14
to ginkgo-a...@googlegroups.com
I typically have a test suite per package.  I also, typically, only test external (exported) behavior (this is why `ginkgo generate` generates files in the _test package) -- I prefer this because testing behavior leads to better code/test decoupling and allows you to refactor under the hood.  When private methods become complicated and hard-to-test it's sometimes an indication that you should pull things out into their own package.

With all that said.  It's basically a stylistic point and you can easily drop the _test from the package declaration and have access to all your object internals.  To do this Go requires that your test files live in the same package, so you'll have to split your tests directory out.

To run all your tests, you can just: ginkgo -r at the top level.  This will recursively enter all your packages and run any tests it finds.  It's not as nice (or as fast) as one large suite, but it is the only way to test internals.

Onsi




On Thu, Apr 24, 2014 at 4:48 PM, <jor...@wastedintelligence.com> wrote:
Up to this point I've been putting all of my tests into a "tests" directory and testing packages externally. That being said, I'd like to be able to access package internals to simplify my tests; I don't want to have to examine state externally, since some of these checks would be somewhat exhaustive. I'm currently exporting functions that I'd rather not, just for the sake of testing.

I'd like to move my tests into the packages they're testing so that I can stop exporting internal functions. Doing that is relatively straight-forward, but maintaining the tie-in to my project-wide "suite test"/ginkgo setup such that "go test" works isn't obvious. Can someone shed some light on how I can have these tests scattered throughout my project, but still be able to run the entire suite?

--
https://github.com/onsi/ginkgo
https://github.com/onsi/gomega
---
You received this message because you are subscribed to the Google Groups "Ginkgo And Gomega" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ginkgo-and-gom...@googlegroups.com.
To post to this group, send email to ginkgo-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/ginkgo-and-gomega.
To view this discussion on the web visit https://groups.google.com/d/msgid/ginkgo-and-gomega/467a761e-da8c-46db-99ac-c2fc541b6e20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages