Repository: xanzy/go-gitlab
Pull request #943 (2020-09-16 11:25:06)

Pipeline tests
- Added test for getPipelineTestReport
- Fixed bug exposed by the tests (is only one test report returned by gitlab API)
- Corrected typo of a struct 


Patch:
diff --git a/pipelines.go b/pipelines.go
index a8edd24..1f1ab28 100644
--- a/pipelines.go
+++ b/pipelines.go
@@ -88,11 +88,11 @@ type PipelineTestReport struct {
 	FailedCount  int                  `json:"failed_count"`
 	SkippedCount int                  `json:"skipped_count"`
 	ErrorCount   int                  `json:"error_count"`
-	TestSuites   []PipelintTestSuites `json:"test_suites"`
+	TestSuites   []PipelineTestSuites `json:"test_suites"`
 }
 
-// PipelintTestSuites contains test suites results.
-type PipelintTestSuites struct {
+// PipelineTestSuites contains test suites results.
+type PipelineTestSuites struct {
 	Name         string              `json:"name"`
 	TotalTime    int                 `json:"total_time"`
 	TotalCount   int                 `json:"total_count"`
@@ -226,7 +226,7 @@ func (s *PipelinesService) GetPipelineVariables(pid interface{}, pipeline int, o
 // GetPipelineTestReport gets the test report of a single project pipeline.
 //
 // GitLab API docs: https://docs.gitlab.com/ee/api/pipelines.html#get-a-pipelines-test-report
-func (s *PipelinesService) GetPipelineTestReport(pid interface{}, pipeline int) ([]*PipelineTestReport, *Response, error) {
+func (s *PipelinesService) GetPipelineTestReport(pid interface{}, pipeline int) (*PipelineTestReport, *Response, error) {
 	project, err := parseID(pid)
 	if err != nil {
 		return nil, nil, err
@@ -238,8 +238,8 @@ func (s *PipelinesService) GetPipelineTestReport(pid interface{}, pipeline int)
 		return nil, nil, err
 	}
 
-	var p []*PipelineTestReport
-	resp, err := s.client.Do(req, &p)
+	p := new(PipelineTestReport)
+	resp, err := s.client.Do(req, p)
 	if err != nil {
 		return nil, resp, err
 	}


Test patch:
diff --git a/pipelines_test.go b/pipelines_test.go
index f00a34d..4c42a53 100644
--- a/pipelines_test.go
+++ b/pipelines_test.go
@@ -68,6 +68,49 @@ func TestGetPipelineVariables(t *testing.T) {
 	}
 }
 
+func TestGetPipelineTestReport(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/pipelines/123456/test_report", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		mustWriteHTTPResponse(t, w, "testdata/get_pipeline_testreport.json")
+	})
+
+	testreport, _, err := client.Pipelines.GetPipelineTestReport(1, 123456)
+	if err != nil {
+		t.Errorf("Pipelines.GetPipelineTestReport returned error: %v", err)
+	}
+
+	want := &PipelineTestReport{
+		TotalTime:    1,
+		TotalCount:   2,
+		SuccessCount: 3,
+		FailedCount:  4,
+		SkippedCount: 5,
+		ErrorCount:   6,
+		TestSuites: []PipelineTestSuites{{
+			Name:         "foo",
+			TotalTime:    1,
+			TotalCount:   2,
+			SuccessCount: 3,
+			FailedCount:  4,
+			SkippedCount: 5,
+			ErrorCount:   6,
+			TestCases: []PipelineTestCases{{
+				Status:        "success",
+				Name:          "bar",
+				Classname:     "class_foo",
+				ExecutionTime: 1,
+				SystemOutput:  "",
+				StackTrace:    "",
+			}},
+		}}}
+	if !reflect.DeepEqual(want, testreport) {
+		t.Errorf("Pipelines.GetPipelineTestReport returned %+v, want %+v", testreport, want)
+	}
+}
+
 func TestCreatePipeline(t *testing.T) {
 	mux, server, client := setup(t)
 	defer teardown(server)
diff --git a/testdata/get_pipeline_testreport.json b/testdata/get_pipeline_testreport.json
new file mode 100644
index 0000000..bdfa92a
--- /dev/null
+++ b/testdata/get_pipeline_testreport.json
@@ -0,0 +1,29 @@
+{
+    "total_time": 1,
+    "total_count": 2,
+    "success_count": 3,
+    "failed_count": 4,
+    "skipped_count": 5,
+    "error_count": 6,
+    "test_suites": [
+        {
+            "name": "foo",
+            "total_time": 1,
+            "total_count": 2,
+            "success_count": 3,
+            "failed_count": 4,
+            "skipped_count": 5,
+            "error_count": 6,
+            "test_cases": [
+                {
+                    "status": "success",
+                    "name": "bar",
+                    "classname": "class_foo",
+                    "execution_time": 1,
+                    "system_output": null,
+                    "stack_trace": null
+                }
+            ]
+        }
+    ]
+}