|
1 | | -# UTBot Go |
| 1 | +# UnitTestBot Go |
2 | 2 |
|
3 | | -## About project |
| 3 | +UnitTestBot Go automatically generates ready-to-use unit tests for Go programs. |
4 | 4 |
|
5 | | -UTBot Go _**automatically generates unit tests for Go programs**_. Generated tests: |
| 5 | +With UnitTestBot Go, you can find bugs in your code and fixate the desired program behavior with less effort. |
6 | 6 |
|
7 | | -* provide _high code coverage_ and, as a result, its reliability; |
8 | | -* fixate the current behavior of the code as _regression tests_. |
| 7 | +The project is under development, |
| 8 | +so feel free to [contribute](https://github.com/UnitTestBot/UTBotJava/blob/main/utbot-go/docs/DEVELOPERS_GUIDE.md). |
9 | 9 |
|
10 | | -The core principles of UTBot Go are _**ease of use**_ and _**maximizing code coverage**_. |
| 10 | +## Features and details |
11 | 11 |
|
12 | | -*** |
| 12 | +UnitTestBot Go now implements the _basic fuzzing technique_. |
| 13 | +It generates input values with respect to parameter types, |
| 14 | +inserts these values into the user functions, and executes the resulting test cases. |
13 | 15 |
|
14 | | -_The project is currently under development._ |
| 16 | +### Supported types for function parameters |
15 | 17 |
|
16 | | -## Features |
| 18 | +At the moment, UnitTestBot Go is able to generate values for _primitive types_, _arrays_ and _structs_. |
17 | 19 |
|
18 | | -At the moment, only the _basic fuzzing technique_ is supported: namely, the execution of functions on predefined values, |
19 | | -depending on the type of parameter. |
| 20 | +For _floating point types_, UnitTestBot Go supports working with _infinity_ and _NaN_. |
20 | 21 |
|
21 | | -At the moment, functions are supported, the parameters of which have _any primitive types_, _arrays_ and _structs_. |
| 22 | +### Supported types for the returned results |
22 | 23 |
|
23 | | -For floating point types, _correct work with infinities and NaNs_ is also supported. |
| 24 | +For the returned function results, |
| 25 | +UnitTestBot Go supports the `error` type in addition to all the types supported for the function parameters. |
24 | 26 |
|
25 | | -Function result types are supported the same as for parameters, but with _support for types that implement `error`_. |
| 27 | +It also captures `panic` cases correctly. |
26 | 28 |
|
27 | | -In addition, UTBot Go correctly captures not only errors returned by functions, but also _`panic` cases_. |
| 29 | +Check the examples of [supported functions](https://github.com/UnitTestBot/UTBotJava/blob/main/utbot-go/go-samples/simple/samples.go). |
28 | 30 |
|
29 | | -Examples of supported functions can be found [here](go-samples/simple/samples.go). |
| 31 | +### Keeping tests near the source code |
30 | 32 |
|
31 | | -## Important notes |
| 33 | +[Testing code typically |
| 34 | +lives in the same package as the code it tests](https://gobyexample.com/testing). |
| 35 | +By default, UnitTestBot Go generates tests into the `[name of source file]_go_ut_test.go` file located in the same |
| 36 | +directory and Go package as the corresponding source file. |
32 | 37 |
|
33 | | -### Where are tests generated? |
| 38 | +If you need to change the location for the generated tests, |
| 39 | +use the `generateGo` CLI command and set the generated test output mode to |
| 40 | +`StdOut` (`-p, --print-test` flag). |
| 41 | +Then, with bash primitives, redirect the output to an arbitrary file. |
34 | 42 |
|
35 | | -It is true that in the described API it is currently almost impossible to customize the file in which the tests are |
36 | | -generated. By default, test generation results in the file `[name of source file]_go_ut_test.go` _located in the same |
37 | | -directory and Go package_ as the source file. |
| 43 | +### Requirements to source code |
38 | 44 |
|
39 | | -In other words, tests are generated right next to the source code. But why? |
| 45 | +To simplify handling dependencies and generating tests, UnitTestBot Go requires the code under test _to |
| 46 | +be a part of a Go project_ consisting of a module and packages. |
40 | 47 |
|
41 | | -* Go was created for convenient and fast development, therefore it has appropriate guidelines: `Testing code typically |
42 | | - lives in the same package as the code it tests` ([source](https://gobyexample.com/testing)). For example, this |
43 | | - approach provides a clear file structure and allows you to run tests as simply and quickly as possible. |
44 | | -* Placing tests in the same package with the source code allows you to test private functions. Yes, this is not good |
45 | | - practice in programming in general: but, again, it allows you to develop in Go faster by automatically checking even |
46 | | - the internal implementation of the public API of the package via unit testing. |
47 | | -* This approach avoids problems with dependencies from imported packages etc. It's always nice not to have them, if |
48 | | - possible. |
| 48 | +To create a simple project, refer to the [starter tutorial](https://go.dev/doc/tutorial/getting-started) if necessary. |
49 | 49 |
|
50 | | -Of course, Go has the ability to store tests away from the source code. In the future, it is planned to support this |
51 | | -functionality in the UTBot Go. |
| 50 | +For larger projects, try the [Create a Go module](https://go.dev/doc/tutorial/create-module) |
| 51 | +and [Call your code from another module](https://go.dev/doc/tutorial/call-module-code) tutorials. |
52 | 52 |
|
53 | | -However, the word `almost` in the first sentence of this section is not redundant at all, there is _a small hack_. When |
54 | | -using the `generateGo` CLI command, you can set the generated tests output mode to StdOut (`-p, --print-test` flag). |
55 | | -Then using, for example, bash primitives, you can redirect the output to an arbitrary file. Such a solution will not |
56 | | -solve possible problems with dependencies, but will automatically save the result of the generation in the right place. |
| 53 | +To create a new module rooted in the current directory, use the `go mod init` command. |
57 | 54 |
|
58 | | -### Is there any specific structure of Go source files required? |
| 55 | +To add missing module requirements necessary to build the current module’s packages and dependencies, |
| 56 | +use the `go mod tidy` command. For editing and formatting `go.mod` files, use the `go mod edit` command. |
59 | 57 |
|
60 | | -Yes, unfortunately or fortunately, it is required. Namely, the source code file for which the tests are generated _must |
61 | | -be in a Go project_ consisting of a module and packages. |
| 58 | +In the future, we plan to make UnitTestBot Go working with arbitrary code as input and generate the simplest |
| 59 | +Go projects automatically. |
62 | 60 |
|
63 | | -But do not be afraid! Go is designed for convenient and fast development, so <ins>_it's easy to start a Go |
64 | | -project_</ins>. For example, the [starter tutorial](https://go.dev/doc/tutorial/getting-started) of the language just |
65 | | -tells how to create the simplest project in Go. For larger projects, it is recommended to read a couple of sections of |
66 | | -the tutorial further: [Create a Go module](https://go.dev/doc/tutorial/create-module) |
67 | | -and [Call your code from another module](https://go.dev/doc/tutorial/call-module-code). |
68 | | - |
69 | | -To put it simply and briefly, in the simplest case, it is enough to use one call to the `go mod init` command. For more |
70 | | -complex ones, `go mod tidy` and `go mod edit` may come in handy. Finally, when developing in IntelliJ IDEA, you almost |
71 | | -don’t have to think about setting up a project: it will set everything up by itself. |
72 | | - |
73 | | -But <ins>_why does UTBot Go need a Go project_</ins> and not enough files in a vacuum? The answer is simple — |
74 | | -dependencies. Go modules are designed to conveniently support project dependencies, which are simply listed in |
75 | | -the `go.mod` file. Thanks to it, modern Go projects are easy to reproduce and, importantly for UTBot Go, to test. |
76 | | - |
77 | | -In the future, it is planned to add the ability to accept arbitrary code as input to UTBot Go and generate the simplest |
78 | | -Go project automatically. |
79 | | - |
80 | | -## Install and use easily |
| 61 | +## Installation and usage |
81 | 62 |
|
82 | 63 | ### IntelliJ IDEA plugin |
83 | 64 |
|
84 | | -<ins>_Requirements:_</ins> |
| 65 | +#### Requirements |
85 | 66 |
|
86 | | -* `IntelliJ IDEA (Ultimate Edition)`, compatible with version `2022.2`; |
87 | | -* installed `Go SDK` version later than `1.18`; |
88 | | -* installed in IntelliJ IDEA [Go plugin](https://plugins.jetbrains.com/plugin/9568-go), compatible with the IDE |
89 | | - version (it is for this that the `Ultimate` edition of the IDE is needed); |
90 | | -* properly configured Go module for source code file (i.e. for file to generate tests for): corresponding `go.mod` file |
91 | | - must exist; |
92 | | -* installed Go modules `github.com/stretchr/testify/assert` and `golang.org/x/tools@v0.4.0`: fortunately, IDEA will automatically highlight |
93 | | - it and offer to install the first time the tests are generated. |
| 67 | +* IntelliJ IDEA (Ultimate Edition) — versions from 2022.2 to 2022.2.4 |
| 68 | +* Go SDK 1.18 or later |
| 69 | +* Compatible [Go plugin](https://plugins.jetbrains.com/plugin/9568-go) for IntelliJ IDEA |
| 70 | +* Properly configured `go.mod` file for the code under test |
| 71 | +* `github.com/stretchr/testify/assert` Go module installed (IntelliJ IDEA automatically offers to install it as soon as the tests are generated) |
94 | 72 |
|
95 | | -Most likely, if you are already developing Go project in IntelliJ IDEA, then you have already met all the requirements. |
| 73 | +#### Installation |
96 | 74 |
|
97 | | -<ins>_To install the UTBot Go plugin in IntelliJ IDEA:_</ins> |
| 75 | +To install the UnitTestBot Go plugin in IntelliJ IDEA, refer to [UnitTestBot user guide](https://github.com/UnitTestBot/UTBotJava/wiki/Install-or-update-plugin). |
98 | 76 |
|
99 | | -* just find the latest version of [UnitTestBot](https://plugins.jetbrains.com/plugin/19445-unittestbot) in the plugin |
100 | | - market; |
101 | | -* or download zip archive with `utbot-intellij JAR` |
102 | | - from [here](https://github.com/UnitTestBot/UTBotJava/actions/runs/3012565900) and install it in IntelliJ IDEA as |
103 | | - follows from plugins section (yes, you need to select the entire downloaded zip archive, it does not need to be |
104 | | - unpacked). |
105 | | -  |
| 77 | +#### Usage |
106 | 78 |
|
107 | | -Finally, you can <ins>_start using UTBot Go_</ins>: open any `.go` file in the IDE and press `alt + u, alt + t`. After |
108 | | -that, a window will appear in which you can configure the test generation settings and start running it in a couple |
109 | | -of clicks. |
| 79 | +1. In your IntelliJ IDEA, go to **File** > **Settings** > **Tools** > **UnitTestBot** and enable **Experimental languages support**. |
| 80 | +2. Open a `.go` file and press **Alt+Shift+U**. |
| 81 | +3. In the **Generate Tests with UnitTestBot** window, you can configure the settings. |
110 | 82 |
|
| 83 | +### CLI |
111 | 84 |
|
112 | | -### CLI application |
| 85 | +#### Requirements |
113 | 86 |
|
114 | | -<ins>_Requirements:_</ins> |
| 87 | +* Java SDK 11 or later |
| 88 | +* Go SDK 1.18 or later |
| 89 | +* Properly configured `go.mod` file for the code under test |
| 90 | +* GCC as well as `github.com/stretchr/testify/assert` and `golang.org/x/tools@v0.4.0` Go modules installed |
115 | 91 |
|
116 | | -* installed `Java SDK` version `11` or higher; |
117 | | -* installed `Go SDK` version later than `1.18`; |
118 | | -* properly configured Go module for source code file (i.e. for file to generate tests for): corresponding `go.mod` file |
119 | | - must exist; |
120 | | -* installed `gcc` and Go modules `github.com/stretchr/testify/assert` and `golang.org/x/tools@v0.4.0` to run tests. |
| 92 | +#### Installation |
121 | 93 |
|
122 | | -<ins>_To install the UTBot Go CLI application:_</ins> download zip archive containing `utbot-cli JAR` |
123 | | -from [here](https://github.com/UnitTestBot/UTBotJava/actions/runs/3012565900), then extract its content (JAR file) to a |
124 | | -convenient location. |
| 94 | +To install the UnitTestBot Go CLI application, go to GitHub, scroll through the release notes and click **Assets**. |
| 95 | +Download the zip-archive named like **utbot-cli-VERSION**. |
| 96 | +Extract the JAR file from the archive. |
125 | 97 |
|
126 | | -Finally, you can <ins>_start using UTBot Go_</ins> by running the extracted JAR on the command line. Two actions are |
127 | | -currently supported: `generateGo` and `runGo` for generating and running tests, respectively. |
| 98 | +#### Usage |
128 | 99 |
|
129 | | -For example, to find out about all options for actions, run the commands as follows |
130 | | -(`utbot-cli-2022.8-beta.jar` here is the path to the extracted JAR): |
| 100 | +Run the extracted JAR file using a command line: `generateGo` and `runGo` actions are supported for now. |
| 101 | +To find info about the options for these actions, |
| 102 | +insert the necessary JAR file name instead of `utbot-cli-2022.8-beta.jar` in the example and run the following commands: |
131 | 103 |
|
132 | 104 | ```bash |
133 | 105 | java -jar utbot-cli-2022.8-beta.jar generateGo --help |
134 | 106 | ``` |
135 | | - |
136 | 107 | or |
137 | | - |
138 | 108 | ```bash |
139 | 109 | java -jar utbot-cli-2022.8-beta.jar runGo --help |
140 | 110 | ``` |
141 | 111 |
|
142 | | -respectively. |
143 | | - |
144 | | -<ins>_Action `generateGo` options:_</ins> |
145 | | - |
146 | | -* `-s, --source TEXT`, _required_: specifies Go source file to generate tests for. |
147 | | -* `-f, --function TEXT`, _required_: specifies function name to generate tests for. Can be used multiple times to select multiple |
148 | | - functions at the same time. |
149 | | -* `-go, --go-path TEXT`, _required_: specifies path to Go executable. For example, it could be `/usr/local/go/bin/go` |
150 | | - for some systems. |
151 | | -* `-et, --each-execution-timeout INT`: specifies a timeout in milliseconds for each fuzzed function execution. Default is |
152 | | - `1000` ms. |
153 | | -* `-at, --all-execution-timeout INT`: specifies a timeout in milliseconds for all fuzzed function execution. Default is |
154 | | - `60000` ms. |
155 | | -* `-p, --print-test`: specifies whether a test should be printed out to StdOut. Is disabled by default. |
156 | | -* `-w, --overwrite`: specifies whether to overwrite the output test file if it already exists. Is disabled by default. |
157 | | -* `-h, --help`: show help message and exit. |
158 | | - |
159 | | -<ins>_Action `runGo` options:_</ins> |
160 | | - |
161 | | -* `-p, --package TEXT`, _required_: specifies Go package to run tests for. |
162 | | -* `-go, --go-path TEXT`, _required_: specifies path to Go executable. For example, it could be `/usr/local/go/bin/go` |
163 | | - for some systems. |
164 | | -* `-v, --verbose`: specifies whether an output should be verbose. Is disabled by default. |
165 | | -* `-j, --json`: specifies whether an output should be in JSON format. Is disabled by default. |
166 | | -* `-o, --output TEXT`: specifies output file for tests run report. Prints to StdOut by default. |
167 | | -* `-cov-mode, --coverage-mode [html|func|json]`: specifies whether a test coverage report should be generated and |
168 | | - defines its mode. Coverage report generation is disabled by default. Examples of different coverage reports modes can |
169 | | - be found [here](go-samples/simple/reports). |
170 | | -* `-cov-out, --coverage-output TEXT`: specifies output file for test coverage report. Required if `[--coverage-mode]` is |
| 112 | +`generateGo` options: |
| 113 | + |
| 114 | +* `-s, --source TEXT`, _required_: specifies a Go source file to generate tests for. |
| 115 | +* `-f, --function TEXT`, _required_: specifies a function name to generate tests for. Can be used multiple times to select multiple |
| 116 | + functions. |
| 117 | +* `-go, --go-path TEXT`, _required_: specifies a path to a Go executable. For example, `/usr/local/go/bin/go`. |
| 118 | +* `-et, --each-execution-timeout INT`: specifies a timeout in milliseconds for each target function execution. |
| 119 | + The default timeout is 1,000 ms. |
| 120 | +* `-at, --all-execution-timeout INT`: specifies a timeout in milliseconds for all target function executions. |
| 121 | + The default timeout is 60,000 ms. |
| 122 | +* `-p, --print-test`: specifies whether a test should be printed out to `StdOut`. Disabled by default. |
| 123 | +* `-w, --overwrite`: specifies whether to overwrite the output test file if it already exists. Disabled by default. |
| 124 | +* `-h, --help`: shows a help message and exits. |
| 125 | + |
| 126 | +`runGo` options: |
| 127 | + |
| 128 | +* `-p, --package TEXT`, _required_: specifies a Go package to run tests for. |
| 129 | +* `-go, --go-path TEXT`, _required_: specifies a path to a Go executable. For example, `/usr/local/go/bin/go`. |
| 130 | +* `-v, --verbose`: specifies whether an output should be verbose. Disabled by default. |
| 131 | +* `-j, --json`: specifies whether an output should be in JSON format. Disabled by default. |
| 132 | +* `-o, --output TEXT`: specifies an output file for a test run report. Prints to `StdOut` by default. |
| 133 | +* `-cov-mode, --coverage-mode [html|func|json]`: specifies whether a test coverage report should be generated and defines the report format. |
| 134 | + Coverage report generation is disabled by default. |
| 135 | +* `-cov-out, --coverage-output TEXT`: specifies the output file for a test coverage report. Required if `[--coverage-mode]` is |
171 | 136 | set. |
172 | 137 |
|
173 | | -## Contribute to UTBot Go |
174 | | - |
175 | | -If you want to _take part in the development_ of the project or _learn more_ about how it works, check |
176 | | -out [DEVELOPERS_GUIDE.md](docs/DEVELOPERS_GUIDE.md). |
177 | | - |
178 | | -For the current list of tasks, check out [FUTURE_PLANS.md](docs/FUTURE_PLANS.md). |
| 138 | +## Contributing to UnitTestBot Go |
179 | 139 |
|
180 | | -Your help and interest is greatly appreciated! |
| 140 | +To take part in project development or learn more about UnitTestBot Go, check |
| 141 | +out the [Developer guide](docs/DEVELOPER_GUIDE.md) and our [future plans](docs/FUTURE_PLANS.md). |
0 commit comments