https://github.com/bytedance/nxt_unit

NxtUnit is an automatically unit test generation application for Go.

https://github.com/bytedance/nxt_unit

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 4 DOI reference(s) in README
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.0%) to scientific vocabulary

Keywords

codegenerator go golang testing unittest
Last synced: 5 months ago · JSON representation

Repository

NxtUnit is an automatically unit test generation application for Go.

Basic Info
  • Host: GitHub
  • Owner: bytedance
  • License: apache-2.0
  • Language: Go
  • Default Branch: main
  • Homepage:
  • Size: 665 KB
Statistics
  • Stars: 76
  • Watchers: 5
  • Forks: 13
  • Open Issues: 7
  • Releases: 1
Topics
codegenerator go golang testing unittest
Created almost 3 years ago · Last pushed almost 2 years ago
Metadata Files
Readme Contributing License

README.md

NxtUnit

NxtUnit is an automatically unit test generation application for Go.\ You can compile it as the binary package and run it.

GitHub license Go codecov

Table of Contents

Introduction

Automated unit test generation has been studied for a long time and prior research has focused on dynamically compiled or dynamically typed programming languages such as Java and Python. However, few of the existing tools support Go, which is a popular statically compiled and typed programming language in the industry for server application development and used extensively in our production environment

NxtUnit is the tool that can automatically generate the unit test for Go. For example, given the original code Go func Example (input1 int, input2 int) { if input1*input2 > 9 { return input1 } switch input1 { case 20: input1 = +RPCCallee1(input2) case 40: input1 = +RPCCallee1(input2) } return input1 }

During the generation, you might see our intermediate code:

```Go package main

import ( "context" "fmt" "io/ioutil" "path" "path/filepath" "reflect" "strings" "sync" "testing" "time"

atgconstant "github.com/bytedance/nxt_unit/atgconstant"
contexthelper "github.com/bytedance/nxt_unit/atghelper/contexthelper"
mockfunc "github.com/bytedance/nxt_unit/codebuilder/mock"
variablecard "github.com/bytedance/nxt_unit/codebuilder/variablecard"
duplicatepackagemanager "github.com/bytedance/nxt_unit/manager/duplicatepackagemanager"
staticcase "github.com/bytedance/nxt_unit/staticcase"
convey "github.com/smartystreets/goconvey/convey"
imports "golang.org/x/tools/imports"

)

func TestDAUAMW(t *testing.T) { wgAUAMW := sync.WaitGroup{} t.Parallel() originPath := "/Users/siweiwang/go/src/github.com/nxt_unit/siwei.go" declLocker := sync.RWMutex{} declData := map[string][]string{} useMockMap := map[string]map[string]int{} type DeclResult struct { AvailableList []bool PathSync sync.Map } declStatistics := map[string]DeclResult{} smartUnitCtx := duplicatepackagemanager.SetInstance(context.Background()) duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "errors") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "context") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "fmt") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "strings") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "sync") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "testing") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "time") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "syscall") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "runtime/debug") duplicatepackagemanager.GetInstance(smartUnitCtx).PutAndGet("", "runtime/stack")

wgAUAMW.Add(1)
go func(t *testing.T) {
    type Args struct {
        Input1 int
        Input2 int
    }
    type test struct {
        Name  string
        Args  Args
        Want  int
        Mocks variablecard.MocksRecord
    }
    defer func() {
        wgAUAMW.Done()
    }()
    tt := test{}
    duplicatepackagemanager.GetInstance(smartUnitCtx).SetRelativePath(tt)
    var rowData []string
    useMock := make(map[string]int, 0)
    for i := 0; i < 4; i++ {
        convey.Convey(tt.Name, t, func() {
            mockRender := &mockfunc.StatementRender{
                MockStatement:   []string{},
                MonkeyOutputMap: make(variablecard.MonkeyOutputMap, 0),
                UsedMockFunc:    make(map[string]int, 0),
            }
            smartUnitCtx = contexthelper.SetVariableContext(smartUnitCtx, atgconstant.VariableContext{})
            tt = variablecard.VariableMutate(smartUnitCtx, reflect.TypeOf(tt), reflect.ValueOf(tt)).Interface().(test)
            defer func() {
            }()
            if got := ExampleAUAMW(tt.Args.Input1, tt.Args.Input2); got != tt.Want {
                tt.Want = got
            }
            tt.Mocks = mockRender.MockStatement
            useMock = mockRender.UsedMockFunc
            rowData = append(rowData, variablecard.ValueToString(smartUnitCtx, reflect.ValueOf(tt)))
        })
    }
    if len(rowData) <= 0 {
        return
    }
    declLocker.Lock()
    declData["Example"] = rowData
    useMockMap["Example"] = useMock
    declLocker.Unlock()
}(t)

// summary data info wait for the result of function
wgAUAMW.Wait()
to := time.After(time.Millisecond * 3000)

Loop: for { select { case info := <-WorkPipeAUAMW: // get declData KeyName declFuncName := info.FunctionName if info.ReceiverName != "" { declFuncName = info.ReceiverName + declFuncName } if info.IsStart != "" { declFuncName = "*" + declFuncName } if _, dataOk := declData[declFuncName]; dataOk { sResult, ok := declStatistics[declFuncName] if ok { sResult.AvailableList = append(sResult.AvailableList, false) } else { sResult.AvailableList = []bool{false} sResult.PathSync = sync.Map{} } declStatistics[declFuncName] = sResult }

        if info.Coverage >= 0 {
            // trim panic coverage == -1
            if sResult, ok := declStatistics[declFuncName]; ok {
                fmt.Println("pathid " + info.PathID)
                if _, exist := sResult.PathSync.Load(info.PathID); !exist {
                    // record unique path
                    sResult.PathSync.Store(info.PathID, struct{}{})
                    // update available case
                    sResult.AvailableList[len(sResult.AvailableList)-1] = true
                    declStatistics[declFuncName] = sResult
                }
            }
        }
    case <-to:
        break Loop
    }
}
var hitLine int
for _, hit := range HitSetAUAMW {
    if hit > 0 {
        hitLine++
    }
}
fmt.Println(fmt.Sprintf("coverage(%v;%v)-r \n", len(HitSetAUAMW), hitLine))
res := map[string]string{}
for k, v := range declData {
    sResult, ok := declStatistics[k]
    if ok {
        dataList := make([]string, 0)
        for index, available := range sResult.AvailableList {
            if available {
                dataList = append(dataList, v[index])
            }
        }
        if len(dataList) > 0 {
            res[k] = fmt.Sprintf("[]test{%s}", strings.Join(dataList, ","))
        }
    }
}
code, err := staticcase.RecordFinalSuite(smartUnitCtx, originPath, res, useMockMap, 0)
if err != nil {
    t.Fatal(err)
}
code, err = imports.Process("", code, nil)
if err != nil {
    t.Fatal(err)
}
testName := strings.ReplaceAll(filepath.Base(originPath), ".go", "_ATG_test.txt")
testFile := path.Join(filepath.Dir(originPath), testName)
if err := ioutil.WriteFile(testFile, code, atgconstant.NewFilePerm); err != nil {
    t.Fatalf("[render testsuite] has ioutil.WriteFile err: %v", err)
}

} ```

it can generate the unit test like below

Go import ( testing "testing" gomonkey "github.com/agiledragon/gomonkey/v2" convey "github.com/smartystreets/goconvey/convey" ) func TestExampleFunction_URRDGU(t *testing.T) { type Args struct { Input1 int, Input2 int } type test struct { Name string Args Args Want int Mocks func() MonkeyOutputMap map[string][]interface{} } tests := []test{test{ Name: "Alice King", Args: Args{ Input1: 20, Input2: 4, }, Want: 20, Mocks: func() {}, MonkeyOutputMap: map[string][]interface{}{"RPCCallee1": []interface{}{10}}, }, test{ Name: "Eason King", Args: Args{ Input1: 7, Input2: 1, }, Want: 7, Mocks: func() {}, MonkeyOutputMap: map[string][]interface{}{"RPCCallee1": []interface{}{11}}, }} for _, tt := range tests { convey.Convey(tt.Name, t, func() { tt.Mocks() PTNFTPatch := gomonkey.ApplyFuncReturn(RPCCallee1, tt.MonkeyOutputMap["RPCCallee1"][0]) defer PTNFTPatch.Reset() if got := ExampleFunction(tt.Args.Input1, tt.Args.Input2); got != tt.Want { convey.So(got, convey.ShouldResemble, tt.Want) } }) } }

How To Use

Installation

go install github.com/bytedance/nxt_unit@latest

Usage

-function_name(required) function name -receiver_name(optional) the receiver name of your function -receiver_is_star(optional) whether your receiver is a pointer or not -usage(required) option1: generate the unit test option2: generate the template -go(optional) your local go path -file_name(required) absolute go path

Example

go build ./nxt_unit -file_path=[your path] -receiver_name=Decoder -receiver_is_star=true -function_name=Decode -usage=plugin -go=/usr/local/go/bin/go

Run generated unit test

go test xxxx_test.go -gcflags "all=-N -l" -gcflags "all=-N -l" used for unblocking the inlining of the function

Failure Scenarios

The failure might be caused by the following reasons: 1. The function is not exported 2. The fault of the gomonkey 3. You don't have permission to execute the file. Please see the Solution

Solution for the gomonkey

1 download the tool cd `go env GOPATH` git clone https://github.com/eisenxp/macos-golink-wrapper.git 2 rename the link to originallink `` mvgo env GOTOOLDIR/linkgo env GOTOOLDIR`/originallink 3 copy tool to GOTOOLDIR cp go env GOPATH/macos-golink-wrapper/link go env GOTOOLDIR/link 4 authorize link chmod +x go env GOTOOLDIR/link ```

Doumentations

License

NxtUnit is licensed under the terms of the Apache license 2.0. See LICENSE for more information.

Citation

@inproceedings{10.1145/3593434.3593443, author = {Wang, Siwei and Mao, Xue and Cao, Ziguang and Gao, Yujun and Shen, Qucheng and Peng, Chao}, title = {NxtUnit: Automated Unit Test Generation for Go}, year = {2023}, isbn = {9798400700446}, publisher = {Association for Computing Machinery}, address = {New York, NY, USA}, url = {https://doi.org/10.1145/3593434.3593443}, doi = {10.1145/3593434.3593443}, booktitle = {Proceedings of the 27th International Conference on Evaluation and Assessment in Software Engineering}, pages = {176–179}, numpages = {4}, keywords = {Go, Automated Test Generation}, location = {Oulu, Finland}, series = {EASE '23} }

Owner

  • Name: Bytedance Inc.
  • Login: bytedance
  • Kind: organization
  • Location: Singapore

GitHub Events

Total
  • Issues event: 1
  • Watch event: 5
  • Fork event: 2
Last Year
  • Issues event: 1
  • Watch event: 5
  • Fork event: 2

Committers

Last synced: almost 3 years ago

All Time
  • Total Commits: 39
  • Total Committers: 4
  • Avg Commits per committer: 9.75
  • Development Distribution Score (DDS): 0.128
Top Committers
Name Email Commits
caoziguang c****g@b****m 34
maoxue.marissa m****a@b****m 2
lmxx1234567 l****7@h****m 2
PoseidoWang s****g@b****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 2
  • Total pull requests: 18
  • Average time to close issues: N/A
  • Average time to close pull requests: 34 minutes
  • Total issue authors: 2
  • Total pull request authors: 5
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.61
  • Merged pull requests: 13
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • hemanthbsridhar (1)
  • doctor-fun (1)
  • mdimado (1)
Pull Request Authors
  • lmxx1234567 (12)
  • fazledyn-or (6)
  • doublepi123 (1)
  • PoseidoWang (1)
  • testwill (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads: unknown
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
proxy.golang.org: github.com/bytedance/nxt_unit

* Copyright 2022 ByteDance Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Stargazers count: 4.8%
Dependent packages count: 7.0%
Average: 7.5%
Forks count: 9.0%
Dependent repos count: 9.3%
Last synced: 6 months ago