My problem is simple, but the answer remains elusive. Suppose I have a package
package mypackage
func DoTheThing() int {
return 5
}
Now suppose I have a test using in the mypackage_test package
package mypackage_test
import "testing"
import . "mypackage"
func TestDoTheThing(t *testing.T) {
if DoTheThing() != 5 {
t.Error("there was a problem")
}
}
Now I want to know the code coverage of the package mypackage.
$ go test -cover
PASS
coverage: 0.0% of statements
ok /my/path/mypackage 0.002s
It should be 100%. I have tried also
$ go test -v -cover -coverpkg ./... ./...
=== RUN TestDoTheThing
--- PASS: TestDoTheThing (0.00s)
PASS
coverage: 0.0% of statements in ./...
ok /my/path/mypackage 0.002s coverage: 0.0% of statements in ./...
It is not a possibility for me to include the test in mypackage, so I need to know the code coverage of mypackage in this setup.
Thanks for your time.
After much keyboard mashing, I discovered it has to do with the fact that I was simlinking my project directory to $GOPATH/src/.
Copying the project into $GOPATH/src/mypackage and running go test -cover correctly returns 100% coverage.
Related
I am trying to run some tests using CTest.
My CTestTestFile.cmake contains a single test
add_test(NAME test1
COMMAND python script.py args
)
but when I try run ctest it outputs the following
Start 1: NAME
Could not find executable test1
Looked in the following places:
test1
test1.exe
Release/test1
Release/test1.exe
Debug/test1
Debug/test1.exe
MinSizeRel/test1
MinSizeRel/test1.exe
RelWithDebInfo/test1
RelWithDebInfo/test1.exe
Deployment/test1
Deployment/test1.exe
Development/test1
Development/test1.exe
Unable to find executable: test1
1/1 Test #1: NAME .............................***Not Run 0.00 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.14 sec
The following tests FAILED:
1 - NAME (Not Run)
This suggests CTest thinks I'm using the short version of the command, so that my test's name is NAME, the command is test1 and python script.py args are the arguments to the command, when this is of course not the case, as I'm obviously using the long version of the command.
I have CMake version 3.24.1 installed.
The CTest runtime, unlike CMake, accepts only the "basic" signature documented at the end of the usual add_test documentation:
add_test(<name> <command> [<arg>...])
Notice that CTest thought your test was named NAME.
So you have to write:
add_test(test1 "python" "script.py" "args")
I'm building fortran project with cmake and I can't find solution to print to console FRUIT test results, they look something like these:
Test module initialized
. : successful assert, F : failed assert
7.00000000000000 -3.60000000000000 7.00000000000000
FFF
Start of FRUIT summary:
Some tests failed!
-- Failed assertion messages:
[_not_set_]:Expected [7.00000000000000], Got [1.00000000000000]
[_not_set_]:Expected [-3.60000000000000], Got [2.00000000000000]
[_not_set_]:Expected [7.00000000000000], Got [6.00000000000000]
-- end of failed assertion messages.
Total asserts : 3
Successful : 0
Failed : 3
Successful rate: 0.00%
Successful asserts / total asserts : [ 0 / 3 ]
Successful cases / total cases : [ 0 / 0 ]
-- end of FRUIT summary
The output I'm getting with make test looks like:
make test
Running tests...
Test project /home/konrad/Desktop/fortran
Start 1: unit_tests
1/1 Test #1: unit_tests ....................... Passed 0.01 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.01 sec
And since passing cmake tests doesn't mean passing FRUIT ones, I want to print FRUIT file everytime I run tests (just for the sake of making it work). I've tried adding printing commands at the end of test command (like this less), adding
-P ${CMAKE_TEST_DIR}/unit_tests.txt
at the end of add_test, building custom after build commands (which I can't make to run after make test so if you knew how to do that would solve it as well, seems like make test or test is not really a target)
Last part of my cmake file with all the testing code:
add_executable(task ${TASK_SOURCES})
add_executable(tests ${TEST_SOURCES})
enable_testing()
set(run_command "${CMAKE_BINARY_DIR}/tests")
set(UNIT_TEST_NAME "unit_tests.txt")
file(MAKE_DIRECTORY ${CMAKE_TEST_DIR})
add_test( NAME unit_tests
COMMAND sh -c
"rm -f ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME} \
&& ${run_command} \
>> ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME} \
&& less ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME}"
)
I have solved a lack of tests output with custom CMake target that will invoke ctest in verbose mode etc.
e.g.
enable_testing()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
--force-new-ctest-process
--verbose
--output-on-failure
)
The output of cmake tests is captured to a file (Testing/Temporary/LastTest.log in my current project).
cmake tests rely on the return code of the test program, with one test program per test.
If you wish to run a program that is a "driver" for your tests, I recommend to use add_custom_target. This commands will add a target that runs a command of your choice.
For instance:
add_custom_target(Name unit_tests tests)
add_dependencies(unit_tests tests)
I am not sure whether the add_dependencies line is needed in this case though (as tests is a target managed by cmake).
Then, you can run
make unit_tests
and it will run your test driver.
I've installed gulp-npm-test following their documentation, that is, in my gulp directory I've got a file test.js that looks like this:
var gulp = require('gulp')
require('gulp-npm-test')(gulp)
var gulp = require('gulp-npm-test')(gulp, {
withoutNpmRun: false
})
But when I run gulp test I get the output like the following:
[17:12:41] Using gulpfile ~/my-project/gulpfile.js
[17:12:41] Starting 'test'...
> my-project#0.0.1 test /Users/wogsland/my-project
> jest
PASS frontend/tests/components/atoms/InputText.test.js
.
.
(many more Jest test passes, no fails)
.
.
FAIL gulp/tasks/test.js
● Test suite failed to run
Your test suite must contain at least one test.
at onResult (node_modules/jest/node_modules/jest-cli/build/TestRunner.js:192:18)
Test Suites: 1 failed, 135 passed, 136 total
Tests: 135 passed, 135 total
Snapshots: 209 passed, 209 total
Time: 92.237s
Ran all test suites.
npm ERR! Test failed. See above for more details.
What am I missing here? I've go a bunch of tests
Looks like the matcher that figures out which files are tests, matches everything that includes in the filename test.js. The default is [ '**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)' ]. So either adapt this one, its testMatch in your jest settings. Or use testPathIgnorePatterns to exclude the /gulp folder.
I'm creating a simple test within my package directory called reverseTest.go
package main
import "testing"
func TestReverse(t *testing.T) {
cases := []struct {
in, want string
}{
{"Hello, world", "dlrow ,olleH"},
{"Hello, 世界", "界世 ,olleH"},
{"", ""},
}
for _, c := range cases {
got := Reverse(c.in)
if got != c.want {
t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
}
}
}
whenever i try to run it the output is
exampleFolder[no test files]
this is my go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/juan/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
Any help will be greatly appreciated. Thanks!!
Files containing tests should be called name_test, with the _test suffix. They should be alongside the code that they are testing.
To run the tests recursively call go test -v ./...
From How to Write Go Code:
You write a test by creating a file with a name ending in _test.go that contains functions named TestXXX with signature func (t *testing.T). The test framework runs each such function; if the function calls a failure function such as t.Error or t.Fail, the test is considered to have failed.
It's possible you don't have any test files in the root package and running go test -v does not test sub-packages, only the root package.
For example
.
├── Dockerfile
├── Makefile
├── README.md
├── auth/
│ ├── jwt.go
│ ├── jwt_test.go
├── main.go
As you see there are no test files in the root package, only the main.go file. You will get "no test files."
The solution is to test all packages within the current working directory, recursively
go test -v ./...
Or if you use govendor
govendor test +local
Or you can specify which package (directory) to test
go test -v ./packagename
Or test a package recursively
go test -v ./packagename/...
Your test function within your _test file must start with the prefix "Test"
GOOD:
func TestName (
BAD:
func NameTest (
This function will not be executed as a test and results with the reported error
To run all the tests use below command
> go test ./...
//For verbose output use -v flag
> go test -v ./...
I faced same problem.
In addition to previous answers i find an issue when impossible to run test if your package's folder name is testing.
Terminal demonstration of the issue below:
with testing folder name:
~/go/src/testing$ go test
? testing [no test files]
without testing folder name:
~/go/src/testing_someothername$ go test
PASS
ok testing_someothername 0.089s
In my case it was helpful
I faced same problem. I fixing them by appending various packages
go test -v ./ ./2ndpackage ./3rdpackage ./4thpackages
this solved the issue.
Also I added "_" between Test keyword and function name
Test_FuncName
no test files mean you need to rename your test file to reflect the file you want to test.
Example
main.go
main_test.go
Where main.go is the file containing your code. main_test.go is the file containing your test code.
I'm trying to have Hudson compile a Java project of mine, but it keeps failing.
I've started a new job, with "build a free-style software project"
I've set the SCM to Mercurial, added a build step "mvn compile" and had it publish Junit and JDepend results. JUnit test report simple named "*.xml"
The cloning of the repo works, but the repo refuses to compile. I get this error message:
[workspace] $ /usr/local/bin/hg log --rev . --template {node}
[workspace] $ /bin/bash -xe /tmp/hudson1162267116265588070.sh
+ mvn compile
/tmp/hudson1162267116265588070.sh: line 2: mvn: command not found
Recording test results
[JDepend] JDepend plugin is ready
[JDepend] Starting JDepend file, outputting to /tmp/jdepend6348540211061861772.xml
[JDepend] Found 2 classes in 1 packages
Finished: FAILURE
EDIT: result of mvn -ver
Java version: 1.6.0_21
Java home: /usr/java/jdk1.6.0_21/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "2.6.32-25-generic" arch: "i386" Family: "unix"
Also, what I added to my /etc/bash.bashrc file.
export JAVA_HOME=/usr/java/jdk1.6.0_21
export M2_HOME=/usr/local/apache-maven/apache-maven-2.2.1
export M2=$M2_HOME/bin
export MAVEN_OPTS="-Xms256m -Xmx512m"
export PATH=$M2:$PATH
export MAVEN_HOME=/usr/local/apache-maven/apache-maven-2.2.1
export MAVEN=$MAVEN_HOME/bin
And the $PATH echo.
/usr/local/apache-maven/apache-maven-2.2.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/java/jdk1.6.0_21/bin
The essential part of John V's answer and its comments.
It can't find maven, because it is not in the path. If you set the path in Hudson's .profile you are out of luck. The profile will only be executed for login shells. AFAIK, Hudson opens a non-interactive shell to execute the build steps and therefore, the .profile will not be executed.
When you login using the Hudson user account and run the maven command you will not have any problems, since the profile is correctly executed.