nxt_unit

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

NxtUnit

NxtUnit is an automatically unit test generation application for Go, you can compile it as the binary package and run it

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

func Example (input1 int, input2 int) 
int {
   if input1*input2 > 9 
   {
      return input1
   }
   switch input1 
   {
   case 20:
      input1 = +RPCCallee1(input2)
   case 40:
      input1 = +RPCCallee1(input2)
   }
   return input1
}

it can generate the unit test like below

import (
   testing "testing"
   gomonkeyv2 "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 := gomonkeyv2.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 https://github.com/bytedance/nxt_unit@latest
Explanation of the parameters
-function_name means your function name
-receiver_name means the receiver name
-receiver_is_star means whether your receiver is a pointer or not.
-usage has two version: plugin is to generate the unit test. -pluginq is to generate the template
-go is your local go path
-file_name is your absolute go path
Generate the unit test
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

Remenber to add go build flag: -gcflags "all=-N -l"

Generation-Failure

solutions

【panic: permission denied】if your GoVersion>=1.17 on macOS Catalina 10.15.x

1.download the tool

cd go env GOPATH git clone https://github.com/eisenxp/macos-golink-wrapper.git

mv go env GOTOOLDIR/link go env GOTOOLDIR/original_link

3. copy tool to GOTOOLDIR

cp go env GOPATH/macos-golink-wrapper/link go env GOTOOLDIR/link

chmod +x go env GOTOOLDIR/link

License

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

Documentation

Overview

* 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.

Directories

Path Synopsis
atg
mockpkg
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
mockpkg/dao
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
mockpkg/duplicateredis/redis
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
mockpkg/redis
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
template
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
template/constructors
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
* Copyright 2022 Bytedance Ltd.
* Copyright 2022 Bytedance Ltd.
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
contexthelper
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
extracinfo
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
testase/user_security
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
utils
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
codebuilder
astutil
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
instrumentation
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
mock
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
setup
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
setup/graph
This file may have been modified by Bytedance Ltd.
This file may have been modified by Bytedance Ltd.
setup/parsermodel
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
stratagy
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
unitestframwork/statement
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
unitestframwork/testcase
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
variablecard
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
MIT License
MIT License
manager
duplicatepackagemanager
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
lifemanager
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
logextractor
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
reporter
* Copyright 2022 ByteDance Inc.
* Copyright 2022 ByteDance Inc.
Package gotests contains the core logic for generating table-driven tests.
Package gotests contains the core logic for generating table-driven tests.
internal/goparser
This file may have been modified by Bytedance Ltd.
This file may have been modified by Bytedance Ltd.
internal/models
This file may have been modified by Bytedance Ltd.
This file may have been modified by Bytedance Ltd.
internal/output
Package goparse contains logic for parsing Go files.
Package goparse contains logic for parsing Go files.
internal/render
This file may have been modified by Bytedance Ltd.
This file may have been modified by Bytedance Ltd.
internal/render/bindata
Code generated for package bindata by go-bindata DO NOT EDIT.
Code generated for package bindata by go-bindata DO NOT EDIT.
templates
Code generated by "esc -include=.*\.tmpl -o=tmpl.go -pkg=templates ./"; DO NOT EDIT.
Code generated by "esc -include=.*\.tmpl -o=tmpl.go -pkg=templates ./"; DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL