Skip to content

Commit

Permalink
feat: Add test cases listing flag
Browse files Browse the repository at this point in the history
  • Loading branch information
chonla committed Jan 17, 2025
1 parent af544cf commit 307e5cf
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 7 deletions.
8 changes: 7 additions & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ permalink: /cli
## Usage

{% highlight bash %}
cotton [-d] [-c] [-p] [-i] [-b <basedir>] [-r <reporttype>] <testpath|testdir>
cotton [-d] [-c] [-p] [-i] [-l] [-b <basedir>] [-r <reporttype>] <testpath|testdir>
cotton -v
cotton --help

Expand All @@ -20,6 +20,7 @@ permalink: /cli
-d debug mode
-h display this help
-i disable certificate verification
-l display test case titles, no test execution
-p paranoid mode
-r string
set reporter type, html or ctrf
Expand Down Expand Up @@ -51,6 +52,11 @@ Cotton prints out help information.

Insecure mode will disable certificate verification.

### `-l` List test cases
`Since 1.2.0`

List all test cases. No test cases are executed.

### `-p` Paranoid mode

Paranoid mode is detailed verbose logging execution. Cotton prints out very detailed information for further debugging purpose.
Expand Down
1 change: 1 addition & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Logger interface {
PrintSectionedMessage(section, message string) error
PrintTestcaseSequence(index, total int) error
PrintTestcaseTitle(title string) error
PrintTestcaseTitleWithPath(title, path string) error
PrintSectionTitle(sectionTitle string) error
PrintExecutableTitle(title string) error
PrintTestResult(passed bool) error
Expand Down
5 changes: 5 additions & 0 deletions internal/logger/logger_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type MockLogger struct {
mock.Mock
}

func (m *MockLogger) PrintTestcaseTitleWithPath(title, path string) error {
args := m.Called(title, path)
return args.Error(0)
}

func (m *MockLogger) PrintTestcaseTitle(title string) error {
args := m.Called(title)
return args.Error(0)
Expand Down
4 changes: 4 additions & 0 deletions internal/logger/nil_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (c *NilLogger) PrintTestcaseTitle(title string) error {
return nil
}

func (c *NilLogger) PrintTestcaseTitleWithPath(title, path string) error {
return nil
}

func (c *NilLogger) PrintExecutableTitle(title string) error {
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions internal/logger/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func (c *TerminalLogger) PrintTestcaseTitle(title string) error {
return err
}

func (c *TerminalLogger) PrintTestcaseTitleWithPath(title, path string) error {
var err error
if c.level == Compact {
_, err = fmt.Printf("%s (%s)", color.New(color.FgHiWhite).Sprint(title), color.New(color.FgWhite).Sprint(path))
} else {
_, err = fmt.Printf("%s (%s)\n", color.New(color.FgHiWhite).Sprint(title), color.New(color.FgWhite).Sprint(path))
}

return err
}

func (c *TerminalLogger) PrintSectionTitle(sectionTitle string) error {
if c.level == Compact {
return nil
Expand Down
2 changes: 2 additions & 0 deletions internal/testcase/full_execution_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ secret=updatedValue`, executableOptions)

eqOp, _ := assertion.NewOp("==")
expectedTestcase := testcase.NewTestcase("Test GET on httpbin.org", "Test getting data from httpbin.org using multiple http requests.", "GET https://httpbin.org/get?key1=value1&key2=value2 HTTP/1.1", testcaseOptions)
expectedTestcase.SetFilename("../../etc/examples/httpbin.org/get.md")
expectedTestcase.AddSetup(expectedSetup)
expectedTestcase.AddTeardown(expectedTeardown)
expectedTestcase.AddAssertion(assertion.New("Body.args.key1", eqOp, "value1"))
Expand Down Expand Up @@ -176,6 +177,7 @@ secret=updatedValue`, executableOptions)

eqOp, _ := assertion.NewOp("==")
expectedTestcase := testcase.NewTestcase("Test GET on httpbin.org with three-tilded code block", "Test getting data from httpbin.org using multiple http requests.", "GET https://httpbin.org/get?key1=value1&key2=value2 HTTP/1.1", testcaseOptions)
expectedTestcase.SetFilename("../../etc/examples/httpbin.org/3tildes.md")
expectedTestcase.AddSetup(expectedSetup)
expectedTestcase.AddTeardown(expectedTeardown)
expectedTestcase.AddAssertion(assertion.New("Body.args.key1", eqOp, "value1"))
Expand Down
7 changes: 6 additions & 1 deletion internal/testcase/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func (p *Parser) FromMarkdownFile(mdFileName string) (*Testcase, error) {
if err != nil {
return nil, err
}
return p.FromMarkdownLines(lines)
tc, err := p.FromMarkdownLines(lines)
if err != nil {
return nil, err
}
tc.SetFilename(mdFileName)
return tc, err
}

func (p *Parser) FromMarkdownLines(mdLines []line.Line) (*Testcase, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/testcase/parser_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Content-Length: 43
Content-Type: application/json`, executableOptions)
expectedTestcase := testcase.NewTestcase("Full documented testcase", "The testcase is described by providing paragraphs right after the test case title.", `GET https://fakestoreapi.com/products HTTP/1.1
Authorization: Bearer {{access_token}}`, testcaseOptions)
expectedTestcase.SetFilename("../../etc/examples/fakestoreapi.com/testcases/full_documented.md")
expectedTestcase.AddSetup(expectedSetup1)
expectedTestcase.AddSetup(expectedSetup2)
expectedTestcase.AddTeardown(expectedTeardown)
Expand Down
16 changes: 16 additions & 0 deletions internal/testcase/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("Title", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -118,6 +119,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("Title", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -175,6 +177,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("Title", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -233,6 +236,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("Title", "Wonderful\nworld", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -295,6 +299,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("Title", "Wonderful", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -348,6 +353,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -411,6 +417,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -474,6 +481,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -527,6 +535,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -630,6 +639,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -699,6 +709,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -759,6 +770,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")

parser := testcase.NewParser(options)
result, err := parser.FromMarkdownFile("mock_file")
Expand Down Expand Up @@ -829,6 +841,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")
expectedTestcase.AddSetup(mockSetup1)
expectedTestcase.AddSetup(mockSetup2)

Expand Down Expand Up @@ -899,6 +912,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")
expectedTestcase.AddTeardown(mockTeardown1)
expectedTestcase.AddTeardown(mockTeardown2)

Expand Down Expand Up @@ -954,6 +968,7 @@ Host: url
post
body`
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")
expectedTestcase.AddCapture(capture.New("varname", "$.result"))

parser := testcase.NewParser(options)
Expand Down Expand Up @@ -1011,6 +1026,7 @@ post
body`
eqOp, _ := assertion.NewOp("==")
expectedTestcase := testcase.NewTestcase("", "", expectedRawRequest, testcaseOptions)
expectedTestcase.SetFilename("mock_file")
expectedTestcase.AddAssertion(assertion.New("$.var", eqOp, float64(3)))
expectedTestcase.AddAssertion(assertion.New("$.var2", eqOp, "good.vibe"))

Expand Down
10 changes: 10 additions & 0 deletions internal/testcase/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Testcase struct {
title string
description string
reqRaw string
filename string
captures []*capture.Capture
setups []*executable.Executable
teardowns []*executable.Executable
Expand All @@ -42,6 +43,7 @@ func NewTestcase(title, description, reqRaw string, options *TestcaseOptions) *T
title: title,
description: description,
reqRaw: reqRaw,
filename: "",
captures: []*capture.Capture{},
setups: []*executable.Executable{},
teardowns: []*executable.Executable{},
Expand Down Expand Up @@ -126,6 +128,14 @@ func (t *Testcase) AddTeardown(teardown *executable.Executable) {
t.teardowns = append(t.teardowns, teardown.Clone())
}

func (t *Testcase) SetFilename(filename string) {
t.filename = filename
}

func (t *Testcase) Filename() string {
return t.filename
}

func (t *Testcase) Execute(passedVars *variable.Variables) *result.TestResult {
testResult := &result.TestResult{
Title: t.Title(),
Expand Down
8 changes: 8 additions & 0 deletions internal/testcase/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func NewTestsuite(path string, options *TestsuiteOptions) (*Testsuite, error) {
}, nil
}

func (ts *Testsuite) PrintTitles() error {
for _, tc := range ts.testcases {
ts.options.Logger.PrintTestcaseTitleWithPath(tc.Title(), tc.Filename())
}

return nil
}

func (ts *Testsuite) Execute() (*result.TestsuiteResult, error) {
initialVars := variable.New()

Expand Down
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
var ver bool
var insecure bool
var stopWhenFailed bool
var listTests bool
var customBaseDir string
var reporterType string

Expand All @@ -34,6 +35,7 @@ func main() {
flag.BoolVar(&detailedDebug, "p", false, "paranoid mode")
flag.BoolVar(&insecure, "i", false, "disable certificate verification")
flag.BoolVar(&stopWhenFailed, "s", false, "stop when test failed")
flag.BoolVar(&listTests, "l", false, "display test case titles, no test execution")
flag.BoolVar(&ver, "v", false, "display cotton version")
flag.BoolVar(&help, "h", false, "display this help")
flag.StringVar(&customBaseDir, "b", "", "set base directory path")
Expand Down Expand Up @@ -122,17 +124,25 @@ func main() {
os.Exit(1)
}

_, err = ts.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
if listTests {
err = ts.PrintTitles()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
_, err = ts.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}

func usage() {
fmt.Fprintf(flag.CommandLine.Output(), `Usage of cotton:
cotton [-d] [-c] [-p] [-i] [-b <basedir>] [-r <reporttype>] <testpath|testdir>
cotton [-d] [-c] [-p] [-i] [-l] [-b <basedir>] [-r <reporttype>] <testpath|testdir>
cotton -v
cotton --help
Expand Down

0 comments on commit 307e5cf

Please sign in to comment.