Include tests in binary and run them

82 views
Skip to first unread message

farid.m...@gmail.com

unread,
Jul 9, 2019, 9:35:44 PM7/9/19
to golang-nuts
We've written some diagnostic tests that we execute during the test phase (go test) however I was wondering if there's an established
pattern for how to include tests in the final binary and execute them afterwards.

The analogous version in Java would be that you could create a "test JAR" which contains the test classes and execute an XUnit framework (JUnit) programmatically yourself run the tests.

Dan Kortschak

unread,
Jul 10, 2019, 12:41:16 AM7/10/19
to farid.m...@gmail.com, golang-nuts
You can ask go test to leave the test executable for you to use later.
This is done with the -c flag. It will leave a <package>-test binary
that takes all the flags that go test takes. This is at least similar
to what you are asking for.

Farid Zakaria

unread,
Jul 10, 2019, 12:43:39 AM7/10/19
to Dan Kortschak, golang-nuts
That's not bad and good to know. 
Thanks for sharing !

Fundamentally I want to include the tests in the main though executable and then run testing.T myself in the normal main
(Through a CLI command)

I'm willing to accept it can't be done because ts not idiomatic but thought I'd inquire. 

Marko Ristin-Kaufmann

unread,
Jul 10, 2019, 8:30:09 AM7/10/19
to farid.m...@gmail.com, golang-nuts
Hi Farid,
If I understood your question correctly, you can play with build flags. Alternatively, you can add a "-diagnostics" flag.


Consider also design-by-contract with contracts tested at run time (e.g., we use our own implementation https://github.com/Parquery/gocontracts; see also other implementations in the readme).

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1441787d-eb2b-479e-821c-934694fbe6b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sean Liao

unread,
Jul 10, 2019, 8:42:55 AM7/10/19
to golang-nuts
I think testing.MainStart() does what you want though do nore the caveats

Sean Liao

unread,
Jul 10, 2019, 10:07:48 AM7/10/19
to golang-nuts
actually looking into it more since *_test,go files aren't included in a normal build you are probably better off just writing your tests as normal functions if you want them included in the final build

Tamás Gulácsi

unread,
Jul 10, 2019, 11:24:16 AM7/10/19
to golang-nuts
2019. július 10., szerda 6:43:39 UTC+2 időpontban Farid Zakaria a következőt írta:
That's not bad and good to know. 
Thanks for sharing !

Fundamentally I want to include the tests in the main though executable and then run testing.T myself in the normal main
(Through a CLI command)

I'm willing to accept it can't be done because ts not idiomatic but thought I'd inquire. 

On Tue, Jul 9, 2019, 9:40 PM Dan Kortschak <d...@kortschak.io> wrote:
You can ask go test to leave the test executable for you to use later.
This is done with the -c flag. It will leave a <package>-test binary
that takes all the flags that go test takes. This is at least similar
to what you are asking for.

On Tue, 2019-07-09 at 18:35 -0700, farid....@gmail.com wrote:
> We've written some diagnostic tests that we execute during the test
> phase
> (go test) however I was wondering if there's an established
> pattern for how to include tests in the final binary and execute
> them
> afterwards.
>
> The analogous version in Java would be that you could create a "test
> JAR"
> which contains the test classes and execute an XUnit framework
> (JUnit)
> programmatically yourself run the tests.
>


go test -c
and append yourpacakge.test onto yourpackage binary as a zip file with github.com/GeertJohan/go.rice/rice .
Reply all
Reply to author
Forward
0 new messages