#Learning go with tests

19 messages · Page 1 of 1 (latest)

hallow locust
#

scratch the thing about integers dir not being in the godoc its probably because all i have in there is a test file. unless thats incorrect XD.

flat surge
#

Typically only have a go.mod at the root of your project - so one is fine!

#

In regards to the code not printing anything to the console, do you mean when running the test or calling the add function from your main function in main.go?

#

A typical convention is to have a separate test file rather than having them bother in the same file. As long as they are in the same package and the function starts with a capital letter it will be accessible in files within the same package

hallow locust
#

Hmm yeah, maybe i’m misunderstanding something it says to put it directly in the _test.go file

flat surge
#

What output do you get when you run go test?

#

The ExampleAdd function should also have the // Output: 6 comment at the end. This is sort of like an assertion.

It will see what is printed to the console and it if matches 6 then the test passes

#

The Add function should be in its own file called adder.go and then test functions and example function should be in adder_test.go

Back when I was using this course the file structure caught me up so don’t be discouraged by it :)

hallow locust
#

I’m pretty sure it doesn’t even run the example. I get no output of 6 from the println and i don’t think i saw it say 2 tests passed. I’ll be able to check in like 5 min. Thanks.

#

this is what i get when i test this code.

hallow locust
#

and this is what my go.mod looks like if it helps or changes anything.

flat surge
#

It won't output it but it will recognise it as a test

#

when you run go test that's not the same as invoking the function itself, there's a difference

#

if you want there to be a log in your console then you need to create a main.go file and call the function there

hallow locust
#

gotcha, i see. with the output it will run the test. without it it says there is no test to run

flat surge
#

it works! nice!

#

Essentially without the Output comment, the go test command doesn't recognise it as a test, because it isn't. it's just a function that can be invoked.

But when you add the output comment the go test runner knows to invoke the function and check to see if it outputs 6

hallow locust
#

Ahhh okay, thanks a lot appreciate that.