Symflower test
This feature executes the tests in your project and outputs test results and coverage information:
symflower test --language=$language --coverage-file=${path to coverage file}
The command takes two arguments:
--language
: Specifies the language used by the repository. Currently, Go, Java, and Ruby are supported.--coverage-file
(optional): If this flag is provided, the command writes to this path the coverage information in JSON.
An example of a valid command:
symflower test --language=golang --coverage-file=coverage.json
symflower test
uses the following tools to execute tests:
- Go:
gotestsum
- Java: Maven
- Ruby: Rake
Note that coverage information is different depending on the tool used for testing. Coverage information generated by the command represents line coverage for Java and Ruby, while it represents branch coverage for Go.
Tutorial: symflower test
Example: a project that handles operations related to geometric shapes.
Project structure:
shapes/
├── area
│ ├── area.go
│ └── area_test.go
├── go.mod
├── go.sum
└── main.go
Function: area.go
contains a function that calculates the area of a circle:
package area
import (
"errors"
"math"
)
func CircleArea(radius float64) (area float64, err error) {
if radius <= 0 {
return 0, errors.New("radius must be a positive value")
}
return math.Pi * radius * radius, nil
}
area_test.go
contains the tests for the CircleArea
function:
package area
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCircleArea(t *testing.T) {
type testCase struct {
Name string
Radius float64
ExpectedArea float64
ExpectedErr error
}
validate := func(t *testing.T, tc *testCase) {
t.Run(tc.Name, func(t *testing.T) {
actualArea, actualErr := CircleArea(tc.Radius)
assert.InDelta(t, actualArea, tc.ExpectedArea, 0.1)
assert.Equal(t, tc.ExpectedErr, actualErr)
})
}
validate(t, &testCase{
Name: "Negative radius",
Radius: -1,
ExpectedErr: errors.New("radius must be a positive value"),
})
validate(t, &testCase{
Name: "Positive radius",
Radius: 5.0,
ExpectedArea: 78.5,
})
}
Let's run symflower test
to run the tests in the project:
symflower test -–language=golang
The command output shows us the test results and the coverage information:
? shapes [no test files]
=== RUN TestCircleArea
=== RUN TestCircleArea/Negative_radius
=== RUN TestCircleArea/Positive_radius
--- PASS: TestCircleArea (0.00s)
--- PASS: TestCircleArea/Negative_radius (0.00s)
--- PASS: TestCircleArea/Positive_radius (0.00s)
PASS
coverage: 100.0% of statements
ok shapes/area 0.002s coverage: 100.0% of statements
DONE 3 tests in 0.285s
By analyzing the output, we can see that all tests in the repository pass and they reach 100% coverage.
Let's run the command again, but this time let's store the coverage information in a file:
symflower test -–language=golang –-coverage-file=coverage.json
The command outputs the same information as the previous run, but this time, a coverage.json
file is created at the root of the project with the following information:
[
{
"FileRange": "shapes/area/area.go:13:2-shapes/area/area.go:13:39",
"CoverageType": "NodeCoverageTrue",
"Count": 1
},
{
"FileRange": "shapes/area/area.go:8:59-shapes/area/area.go:11:3",
"CoverageType": "NodeCoverageTrue",
"Count": 1
}
]
The result indicates complete node coverage:
shapes/area/area.go:8:59-shapes/area/area.go:11:3
: this range indicates theif
block was coveredshapes/area/area.go:13:2-shapes/area/area.go:13:39
: this range indicates the return statement was covered