How do I set up a Ginkgo test suite? - go

I have inherited a Go project that consists of a lot of common files, a library of sorts, two executables, and theoretically a test suite. The test suite is being written after the fact. But I dislike the only way I've found of setting up is rather unpalatable
I'm using Ginkgo, and this is my starting directory structure
component1/component1.go
component2/component2.go
cmd1/cmd1.go
cmd2/cmd2.go
project_suite_test.go
component1_test.go
Each cmd?.go file will be compiled into a separate executable.
What I would like is a multi-file test suite, usually one file per component. Where do I put the files so that go test will find and run all of them, without leaving them here in the root of the project?

ginkgo init and ginkgo bootstrap will set up your tests. ginkgo -r will run all your tests recursively.

Reason:
Ginkgo command will only work if you have actually bootstrap your project via ginkgo.
Options:
To use that you have to go to your test dir in terminal and run
ginkgo init : To Initialise project:
ginkgo bootstrap : This will generate new file with test suite config
ginkgo or ginkgo test : this will now be able to run tests based on your new generated file because that's what it is trying to search.
Alternatively:
If you like to keep your tests in a sub-folder, say test, then running
go test ./...
will attempt to run tests in every folder, even those that do not contain any test, thus having a ? in the subsequent report for non-test folders.
Running
go test ./.../test
instead will target only your test folders, thus having a clean report focused on your tests folders only.
you can alternatively use 'go run $(ls *.go)' to run all the files in a given folder.
Notice you have regular expression within () braces.
In-case you want to run test in different path update path as per your desired dir in the regular expression

You can use go test ./... in the root and it will go into child folders and execute the tests:
component1/component1.go
component1/component1_test.go
component2/component2.go
component2/component2_test.go
cmd1/cmd1.go
cmd1/cmd1_test.go
cmd2/cmd2.go
cmd2/cmd2_test.go

Related

Is there a way to specify which tests to run (from multiple files) in Cypress?

I'm using Javascript with Cypress framework to automate tests.
How do I mark tests as tier1 , tier 2 etc. so that I could run only tests marked tier1 or tier2 ?
You can group them by folders, and then use the ignoreTestFiles and testFiles config params to ignore or use them, according to your needs.
You will have to use the route that matches all the tests inside the folder.
For example, you have three folders inside the integration folder called Tier1, Tier2 and Tier3. To indicate that Cypress should ignore tests inside Tier1 and only use Tier2 and Tier3 you have to add to your config:
ignoreTestFiles: '**/Tier1/*.js'
Or if you prefer:
testFiles: ['**/Tier2/*.js', '**/Tier3/*.js']
To know more about config params take a look to the Cypress docs: https://docs.cypress.io/guides/references/configuration#Folders-Files
Set up different subfolders in your integration folder in Cypress, called "tier1", tier2", tier3"
npx cypress run will still run all subfolders
"npx cypress run --spec "cypress/integration/{subfolder}/*-spec.js" will run all tests in a specific subfolder

How to run tests outside of integration folder in Cypress

I have project B linked to project A with npm link and am trying to run tests from B in A. Project A builds the entire front end and could use other modules than just B and so I want to be able have the test runner use A and its tests but also use tests from the linked project (assuming the linked projects all use similar Cypress directory structures). I first tried this by setting the testFiles attribute in the config to an array like [/path/to/ProjectATestingRoot/integration/**/*.*", "/path/to/ProjectBTestingRoot/integration/**/*.*"]
and running Cypress with integrationFolder to be from project A. While I'm able to see all my tests when I open Cypress, only project A's tests can be run. When I run project B's they get stuck when the browser loads the test and displays the "Your tests are loading..." screen for eternity.
Is there any way that I could run tests from outside the set integration folder? I thought I could write a little plugin to copy the testing files over but that seems more laborious than needed.
Using spec should solve the problem
npx cypress run --spec [abloluteFolderPath}
"testFiles": "**/*.{feature,spec.tsx}",
"integrationFolder": ".",
"ignoreTestFiles": "**/node_modules/**/*{feature,spec.js}"
Add this to your cypress.json. It adds all the files with .spec.tsx ignoring the ones inside the node_module.
https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/unit-testing__application-code
There are 2 options
1 You can specify integrationFolder in the cypress.json
{
//....
"integrationFolder": "cypress/tests"
// or
// "integrationFolder": "tests"
}
More information https://docs.cypress.io/guides/references/configuration#Folders-Files
2 You can specify integration folder for each test run
npx cypress --config integrationFolder=cypress/tests
npx cypress --config integrationFolder=tests
More information https://github.com/cypress-io/cypress/issues/2256#issuecomment-544408366

Running benchmarks of subfolders from the project root

I have a project with a binary which entrypoint is located in ./cmd/<my-project>/main.go.
I've added a benchmark in ./cmd/<my-project>/main_test.go.
Now I want to execute this benchmark from the repository root.
I've tried to execute something like go test -bench=./cmd/<my-project>/main in various alternations (like for example go test -bench=./cmd/<my-project>/main_test.go or go test -bench=./cmd/<my-project>) but I couldn't get it to work.
The error I always get is:
can't load package: package github.com/<username>/<my-project>: no Go files in /home/<user>/go/src/github.com/<username>/<my-project>
and it's true, I have no *.go files in my project root.
All I came up with is changing into the directory first and run the benchmark with `go test -bench=.
However, as my program depends on the current working directory and the result is heavily depending on that it would be great to be able to execute it in another directory than the cmd/<my-project> one. (If this is good or not isn't part of the question :))
Update after the first comment:
I get it to run with go test -bench=. /cmd/<my-project> but the benchmark get's executed as if it would be run in the ./cmd/<my-project> directory.
I've noticed similiar behaviour in 'normal tests' - but that wasn't a problem for me at any time.
So, my program does scan the current directory for files directories depending on the current working directory, this results in only two files found (main.go and main_test.go) where the benchmark isn't really helpful.
So to run the benchmarks:
go test -bench=. ./cmd/<my-project>
However, you are wanting to control the working directory, so really you want to run it from somewhere else. What you are looking for is the -c flag. It will create a binary instead of running the tests. You can then run the binary (and therefore your tests and benchmarks) where you want.
go test -c ./cmd/my_proj
my_proj.test -test.bench=.
NOTE: Flags are prefixed with test. when you compile the tests.

copying to teamcity's out directory before running unit tests

So my situation is that I finally finished configuring TeamCity for CI. I got it to run my unit tests with some friendly help on SO.
However, many unit tests fail because there needs to be a config file alongside the unittests.dll once it's built and ready to run.
I've written a simple Command Line step with:
copy %system.teamcity.build.checkoutDir%\xx.configfile <destination>
The destination is the problem, I need it to be the Out directory teamcity creates.
TC creates SYSTEM_<machinename> <datetime>\OUT. An example:
C:\TeamCity\buildAgent\temp\buildTmp\SYSTEM_GIDEON 2015-07-02 16_51_09\Out
In there is my unittests.dll and I want to copy my config file there. What environment var or (anything else) can I use in the command line script?
The (1) Build Tests is a Step then I want to run the (2) Copy Config Step Then (3) Run Tests. After step (1) I have that xxx\xxx\Out directory and I need that directory from some variable.
I'm using Teamcity 9.0.2
Your problem is not to do with TeamCity I don't think, it's to do with the way that MSTest works. You need your .config file to be a DeploymentItem and have your tests deploy it to the directory that MSTest will run the tests in.
To be honest I'm surprised that you don't have this problem running locally, and it makes me think that you must be using some other test runner (like ReSharper) to run the tests if you have not seen this problem on your local machines.

Check if all tests are successful in `after(:all)`

In before(:all) I set up a temporary directory (#work_dir) where the tests can place their files. I would like to remove this directory inside after(:all), but only if all the tests have been successful. If one of the tests failed I would like the directory to be left intact so I can look at the generated files.
What code can I use inside after(:all) to check whether all the tests have been successful run?
You can write custom bash script, that starts running you test suite.
After that it checks return code, if it is non-zero, it keeps files. In other case it removes.
As a second solution, you can clean your directory before running tests.
In such case, when your test suite is failed, you can go and check files.

Resources