cli_run_unit_test.go 3.1 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package main

/**
$>ztf junit -p 1 mvn clean package test          执行junit单元测试脚本,
*/
import (
	"bufio"
	"fmt"
	"io"
	"os"
	"os/exec"
	"regexp"
	"runtime"
	"strings"
	"testing"

Z
zhaoke 已提交
17
	commonTestHelper "github.com/easysoft/zentaoatf/cmd/test/helper/common"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
18
	constTestHelper "github.com/easysoft/zentaoatf/cmd/test/helper/conf"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32
	fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
	"github.com/go-git/go-git/v5"
	"github.com/ozontech/allure-go/pkg/framework/provider"
	"github.com/ozontech/allure-go/pkg/framework/suite"
)

type RunUnitSuit struct {
	suite.Suite
}

func (s *RunUnitSuit) BeforeEach(t provider.T) {
	if runtime.GOOS == "windows" {
		os.RemoveAll(fmt.Sprintf("%s\\test\\demo\\php\\product1", constTestHelper.RootPath))
	} else {
Z
zhaoke 已提交
33
		os.RemoveAll(fmt.Sprintf("%s/cmd/test/demo/php/product1", constTestHelper.RootPath))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
34
	}
Z
zhaoke 已提交
35
	commonTestHelper.ReplaceLabel(t, "命令行-运行单元测试")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
36 37 38
}

func (s *RunUnitSuit) TestRunTestng(t provider.T) {
Z
zhaoke 已提交
39
	testngDir := fmt.Sprintf("%scmd/test/demo/ci_test_testng", constTestHelper.RootPath)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
40 41 42 43 44 45 46
	t.ID("5432")
	t.Title("执行TestNG单元测试")
	cloneGit("https://gitee.com/ngtesting/ci_test_testng.git", testngDir)
	t.Require().Equal("Success", testRunUnitTest("mvn clean package test", testngDir, regexp.MustCompile(`Tests run\: 3, Failures\: 0, Errors\: 0, Skipped\: 0`)))
}

func (s *RunUnitSuit) TestRunPytest(t provider.T) {
Z
zhaoke 已提交
47
	pytestDir := fmt.Sprintf(".%scmd/test/demo/ci_test_pytest", constTestHelper.RootPath)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	t.ID("5435")
	t.Title("执行PyTest单元测试")
	cloneGit("https://gitee.com/ngtesting/ci_test_pytest.git", pytestDir)

	t.Require().Equal("Success", testRunUnitTest("pytest --junitxml=testresults.xml", pytestDir, regexp.MustCompile("1 failed, 1 passed")))
}

func cloneGit(gitUrl string, name string) error {
	projectDir := name
	fileUtils.MkDirIfNeeded(projectDir)

	options := git.CloneOptions{
		URL:      gitUrl,
		Progress: os.Stdout,
	}
	_, err := git.PlainClone(projectDir, false, &options)
	return err
}

func testRunUnitTest(cmdStr, workspacePath string, successRe *regexp.Regexp) string {

	var cmd *exec.Cmd
	if runtime.GOOS == "windows" {
		cmd = exec.Command("cmd", "/C", cmdStr)
	} else {
		cmd = exec.Command("/bin/bash", "-c", cmdStr)
	}

	cmd.Dir = workspacePath

	if cmd == nil {
		return "cmd is nil"
	}
	stdout, err1 := cmd.StdoutPipe()
	stderr, err2 := cmd.StderrPipe()

	if err1 != nil {
		return err1.Error()
	} else if err2 != nil {
		return err2.Error()
	}
	cmd.Start()

	isTerminal := false
	reader1 := bufio.NewReader(stdout)
	for {
		line, err3 := reader1.ReadString('\n')
		if line != "" {
			isTerminal = true
			if successRe.MatchString(line) {
				return "Success"
			}
		}

		if err3 != nil || io.EOF == err3 {
			break
		}

	}

	errOutputArr := make([]string, 0)
	if !isTerminal {
		reader2 := bufio.NewReader(stderr)

		for {
			line, err2 := reader2.ReadString('\n')
			if err2 != nil || io.EOF == err2 {
				break
			}
			errOutputArr = append(errOutputArr, line)
		}
	}

	errOutput := strings.Join(errOutputArr, "")

	if errOutput != "" {
		return errOutput
	}

	cmd.Wait()

	return "Success"
}

func TestCliRunUnit(t *testing.T) {
	suite.RunSuite(t, new(RunUnitSuit))
}