Run go tests except from one package - go

I’ve the following project structure and I want to run go test exclude option to run the test
e.g. Command to Run all tests except the test in the cmd package (I’ve more then 3 packages , the struct below is a simple example)
myGithubProject/
|---- cmd
|---- -command
|---- -hello.go
|---- -hello_test.go
|---- internal
|---- -fs.go
|---- -fs.go
|---- -fs_test.go
|---- -log
|---- -log.go
|---- -log_test.go
main.go

If you have a hierarchy like that, you may specify the sibling folder to test (and recurse down) like this:
go test internal/...
If this is not feasible to you (e.g. you have many siblings of cmd, or you have many subfolders inside cmd which you do want to test), you may use build constraints to achieve what you want.
For example, add an exclusion of a donttestme build tag to the hello_test.go file (the first line):
// +build !donttestme
And then when you specify this tag when testing, files that exclude this build tag will not be considered (will be skipped):
go test -tags donttestme <somepackages>

Related

How can I use cli Go files that belong to a web app?

My question is related to the structure or approach of Go applications. I have got the following application.
root
|- app
| |- services
| |- repositories
| |- handlers
| |- commands
|- go.mod
|- main.go
The Go files in the commands package are working independently. The rest of the packages are working for a web application. I start an HTTP web server in the main.go
So, I'd like to run the Go files in the commands packages in the crontab. But as I know, I'll build these whole packages into a single binary file. My question is how can I run the Go files in the commands packages independently in the crontab? I think I should separate them into 2 applications such as "web app" and "command app" but actually they are related to each other and I don't want to manage 2 apps differently. May I use commands Go files in the crontab and on the other hand start an HTTP web server in the main.go?
There's no reason why you can't import packages from your web application module into another one, but if you want to keep them together, you can just do what is quite common, and add additional main packages in specific directories, giving a directory structure like this:
root
|- app
| |-services
| |-...
|- cmd
| |- tools
| | |- main.go
|- main.go
You can build/install your CLI binary simply by running go build ./cmd/tools or go install ./cmd/tools

Testing golang folder structure

My folders are structured as follows:
src/
github.com/
hello/
hello.go
hello_test.go
integers/
integers.go
integers_test.go
hello.go/hello_test.go belong to package main
integers.go/integers_test.go belong to package integers
When runing go test from the root of folder hello, it only runs hello_test.go. How can I get it to run all tests recursively. Is this the right way to structure tests inside their respective packages?
How about having a make file? In the future if you expand your project and have more test files then you can test all of them at once by just running make test.
This link would be helpful.

Run go test for specific package

I used the following command to run test for specific package
go test fts -run run_test.go
can't load package: package fts: cannot find package "fts" in any of:/usr/local/Cellar/go/1.11.1/libexec/src/integration (from $GOROOT)/Users/i055555/go/src/fts (from $GOPATH)
And the package looks like
gitproj/
|---- fts
|---- -command
|---- -run.go
|---- -run_test.go
|---- internal
|---- -fs.go
|---- -tb.go
|---- -tb_test.go
main.go
So you can run go test for a specific package by giving it the relative path:
go test ./fts/command
The --run flag takes a regular expression that helps dictates which tests will be ran within the package.
For example if you had a test named TestFoo(...) and another TestBar(...). go test --run=TestFoo will only run TestFoo(...).

Package layout for Go projects using modules to track dependencies?

Go now provides modules for dependency management, and I'm a little bit confused as to how I should organize my projects.
In traditional $GOPATH mode, I would organize an application as follows:
├─ cmd/
| └─ myapp/
| └─ main.go
├─ otherstuff/
| └─ file.go
└─ README.md, etc.
This is what I see most projects on GitHub doing.
However, now that we have modules, I'm not sure where to put go.mod. Does it go in the project's root directory? Does it go in cmd/[whatever]/? Should I still be putting main.go in the cmd/[whatever] directory or should it be in the project's root directory now?
From the wiki:
A module is a collection of related Go packages that are versioned together as a single unit. Most often, a single version-control repository corresponds exactly to a single module, but alternatively, a single version-control repository can hold multiple modules.
So putting go.mod right next to .git (or equivalent for some other VCS) is almost always the right thing to do. You would only create multiple modules in a single repository if the code in each module is truly independent from the other modules, so that the version of one module does not effect the other modules in any way.

Ginkgo package testing

I'm implementing test suite for brand new go app and decided to use ginkgo. The app has main function and several packages
.
|- main.go
|- types
| |-- user.go
| |-- post.go
|- server_pkg
| |-- users_controller.go
| |-- posts_controller.go
|- worker_pkg
| |-- users_worker.go
| |-- posts_worker.go
I ran ginkgo bootstrap in each package folder and added test files using ginkgo generate. Now I'm able to run tests for each package separately i.e.
cd server_pkg; ginkgo
The question is that: how to configure my application to run all tests for main function and packages using single command?
I can chain commands like ginkgo; cd server_pkg; ginkgo ..., but it does not look like good solution.
To run all test suits you should run this in command in your root catalog
ginkgo -r
Also is good practice as in normal test suits to run all test with race detector, also you could shuffle some test. You can run all this option by using
ginkgo -r --race --randomizeAllSpecs --randomizeSuites
#ttomalak thank you! It's exactly what I wanted
$ ginkgo -r

Resources