testza
Full-featured test framework with nice colorized output
About testza
This large README is shown as a plain-text preview. Read the full README at the source
<h1 align="center">testza 🍕</h1>
<p align="center">Testza is like pizza for Go - you could live without it, but why should you?</p>
<p align="center">
<a href="https://github.com/MarvinJWendt/testza/releases">
<img src="https://img.shields.io/github/v/release/MarvinJWendt/testza?style=flat-square" alt="Latest Release">
</a>
<a href="https://codecov.io/gh/MarvinJWendt/testza" target="_blank">
<img src="https://img.shields.io/github/actions/workflow/status/MarvinJWendt/testza/go.yml?label=tests&style=flat-square" alt="Tests">
</a>
<a href="https://codecov.io/gh/MarvinJWendt/testza" target="_blank">
<img src="https://img.shields.io/codecov/c/gh/MarvinJWendt/testza?color=magenta&logo=codecov&style=flat-square" alt="Coverage">
</a>
<a href="https://codecov.io/gh/MarvinJWendt/testza">
<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-2956-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
</a>
<a href="https://pkg.go.dev/github.com/MarvinJWendt/testza" target="_blank">
<img src="https://pkg.go.dev/badge/github.com/MarvinJWendt/testza.svg" alt="Go Reference">
</a>
<a href="https://goreportcard.com/report/github.com/MarvinJWendt/testza" target="_blank">
<img src="https://goreportcard.com/badge/github.com/MarvinJWendt/testza?style=flat-square" alt="Go report">
</a>
</p>
---
<p align="center">
<strong><a href="https://github.com/MarvinJWendt/testza#-installation">Get The Module</a></strong>
|
<strong><a href="https://github.com/MarvinJWendt/testza#-documentation" target="_blank">Documentation</a></strong>
|
<strong><a href="https://github.com/atomicgo/atomicgo/blob/main/CONTRIBUTING.md" target="_blank">Contributing</a></strong>
|
<strong><a href="https://github.com/atomicgo/atomicgo/blob/main/CODE_OF_CONDUCT.md" target="_blank">Code of Conduct</a></strong>
</p>
---
<img align="right" height="400" alt="Screenshot of an example test message" src="https://user-images.githubusercontent.com/31022056/161153895-e772bc61-b751-407f-b526-8f6a66d8f8d5.png" />
<br/>
<p align="center">
<a href="https://discord.gg/vE2dNkfAmF">
<img width="300" src="https://user-images.githubusercontent.com/31022056/158916278-4504b838-7ecb-4ab9-a900-7dc002aade78.png" alt="Join us on Discord!" />
<br/>
<b>Join us on Discord for support, discussions, updates and general talk!</b>
</a>
</p>
## 📦 Installation
```console
# Execute this command inside your project
go get github.com/MarvinJWendt/testza
```
<br/>
<br/>
## 📝 Description
Testza is a full-featured testing framework for Go.
It integrates with the default test runner, so you can use it with the standard `go test` tool.
Testza contains easy to use methods, like assertions, output capturing, fuzzing, and much more.
The main goal of testza is to provide an easy and fun experience writing tests and providing a nice, user-friendly output.
Even developers who never used testza, will get into it quickly.
## ⭐ Features
| Feature | Description |
|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| Assertions | Assertions allow you to quickly check objects for expected values. |
| Fuzzing | Fuzzing allows you to check functions against sets of generated input parameters.<br/>A couple lines of test code can run thousands of sanity tests. |
| Output Capture | Capture and validate output written to the terminal.<br/>Perfect for CLI tools. |
| Snapshots | Snapshot objects between test runs, to ensure a consistent behaviour. |
| Clean Output | Clean and colorful output provides you the needed details in an easy-to-understand format. |
| System Information | Testza prints information about the system on startup.<br/> You can quickly figure out what's wrong, when a user submits an issue. |
| Well Documented | Every function of testza is well documented and contains an example to make usage super easy. |
| Customizable | Testza features customizable settings, if you want to change something. |
| Test flags | You can configure testza via flags too!<br/>That makes it super simple to change test runs, or output, without touching code! |
## 🚀 Getting Started
See the examples below for a quick introduction!
```go
// --- Some Examples ---
// - Some assertions -
testza.AssertTrue(t, true) // -> Pass
testza.AssertNoError(t, err) // -> Pass
testza.AssertEqual(t, object, object) // -> Pass
// ...
// - Testing console output -
// Test the output of your CLI tool easily!
terminalOutput, _ := testza.CaptureStdout(func(w io.Writer) error {fmt.Println("Hello"); return nil})
testza.AssertEqual(t, terminalOutput, "Hello\n") // -> Pass
// - Fuzzing -
// Testing a function that accepts email addresses as a parameter:
// Testset of many different email addresses
emailAddresses := testza.FuzzStringEmailAddresses()
// Run a test for every string in the test set
testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str string) {
user, domain, err := internal.ParseEmailAddress(str) // Use your function
testza.AssertNoError(t, err) // Assert that your function does not return an error
testza.AssertNotZero(t, user) // Assert that the user is returned
testza.AssertNotZero(t, domain) // Assert that the domain is returned
})
// And that's just a few examples of what you can do with Testza!
```
## 📚 Documentation
<!-- docs:start -->
<table>
<tr>
<th>Module</th>
<th>Methods</th>
</tr><tr>
<td><a href="https://github.com/MarvinJWendt/testza#Settings">Settings</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [SetColorsEnabled](https://github.com/MarvinJWendt/testza#SetColorsEnabled)
- [SetDiffContextLines](https://github.com/MarvinJWendt/testza#SetDiffContextLines)
- [SetLineNumbersEnabled](https://github.com/MarvinJWendt/testza#SetLineNumbersEnabled)
- [SetRandomSeed](https://github.com/MarvinJWendt/testza#SetRandomSeed)
- [SetShowStartupMessage](https://github.com/MarvinJWendt/testza#SetShowStartupMessage)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Assert">Assert</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [AssertCompletesIn](https://github.com/MarvinJWendt/testza#AssertCompletesIn)
- [AssertContains](https://github.com/MarvinJWendt/testza#AssertContains)
- [AssertDecreasing](https://github.com/MarvinJWendt/testza#AssertDecreasing)
- [AssertDirEmpty](https://github.com/MarvinJWendt/testza#AssertDirEmpty)
- [AssertDirExists](https://github.com/MarvinJWendt/testza#AssertDirExists)
- [AssertDirNotEmpty](https://github.com/MarvinJWendt/testza#AssertDirNotEmpty)
- [AssertEqual](https://github.com/MarvinJWendt/testza#AssertEqual)
- [AssertEqualValues](https://github.com/MarvinJWendt/testza#AssertEqualValues)
- [AssertErrorIs](https://github.com/MarvinJWendt/testza#AssertErrorIs)
- [AssertFalse](https://github.com/MarvinJWendt/testza#AssertFalse)
- [AssertFileExists](https://github.com/MarvinJWendt/testza#AssertFileExists)
- [AssertGreater](https://github.com/MarvinJWendt/testza#AssertGreater)
- [AssertGreaterOrEqual](https://github.com/MarvinJWendt/testza#AssertGreaterOrEqual)
- [AssertImplements](https://github.com/MarvinJWendt/testza#AssertImplements)
- [AssertInRange](https://github.com/MarvinJWendt/testza#AssertInRange)
- [AssertIncreasing](https://github.com/MarvinJWendt/testza#AssertIncreasing)
- [AssertKindOf](https://github.com/MarvinJWendt/testza#AssertKindOf)
- [AssertLen](https://github.com/MarvinJWendt/testza#AssertLen)
- [AssertLess](https://github.com/MarvinJWendt/testza#AssertLess)
- [AssertLessOrEqual](https://github.com/MarvinJWendt/testza#AssertLessOrEqual)
- [AssertNil](https://github.com/MarvinJWendt/testza#AssertNil)
- [AssertNoDirExists](https://github.com/MarvinJWendt/testza#AssertNoDirExists)
- [AssertNoError](https://github.com/MarvinJWendt/testza#AssertNoError)
- [AssertNoFileExists](https://github.com/MarvinJWendt/testza#AssertNoFileExists)
- [AssertNoSubset](https://github.com/MarvinJWendt/testza#AssertNoSubset)
- [AssertNotCompletesIn](https://github.com/MarvinJWendt/testza#AssertNotCompletesIn)
- [AssertNotContains](https://github.com/MarvinJWendt/testza#AssertNotContains)
- [AssertNotEqual](https://github.com/MarvinJWendt/testza#AssertNotEqual)
- [AssertNotEqualValues](https://github.com/MarvinJWendt/testza#AssertNotEqualValues)
- [AssertNotErrorIs](https://github.com/MarvinJWendt/testza#AssertNotErrorIs)
- [AssertNotImplements](https://github.com/MarvinJWendt/testza#AssertNotImplements)
- [AssertNotInRange](https://github.com/MarvinJWendt/testza#AssertNotInRange)
- [AssertNotKindOf](https://github.com/MarvinJWendt/testza#AssertNotKindOf)
- [AssertNotNil](https://github.com/MarvinJWendt/testza#AssertNotNil)
- [AssertNotNumeric](https://github.com/MarvinJWendt/testza#AssertNotNumeric)
- [AssertNotPanics](https://github.com/MarvinJWendt/testza#AssertNotPanics)
- [AssertNotRegexp](https://github.com/MarvinJWendt/testza#AssertNotRegexp)
- [AssertNotSameElements](https://github.com/MarvinJWendt/testza#AssertNotSameElements)
- [AssertNotUnique](https://github.com/MarvinJWendt/testza#AssertNotUnique)
- [AssertNotZero](https://github.com/MarvinJWendt/testza#AssertNotZero)
- [AssertNumeric](https://github.com/MarvinJWendt/testza#AssertNumeric)
- [AssertPanics](https://github.com/MarvinJWendt/testza#AssertPanics)
- [AssertRegexp](https://github.com/MarvinJWendt/testza#AssertRegexp)
- [AssertSameElements](https://github.com/MarvinJWendt/testza#AssertSameElements)
- [AssertSubset](https://github.com/MarvinJWendt/testza#AssertSubset)
- [AssertTestFails](https://github.com/MarvinJWendt/testza#AssertTestFails)
- [AssertTrue](https://github.com/MarvinJWendt/testza#AssertTrue)
- [AssertUnique](https://github.com/MarvinJWendt/testza#AssertUnique)
- [AssertZero](https://github.com/MarvinJWendt/testza#AssertZero)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Capture">Capture</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [CaptureStderr](https://github.com/MarvinJWendt/testza#CaptureStderr)
- [CaptureStdout](https://github.com/MarvinJWendt/testza#CaptureStdout)
- [CaptureStdoutAndStderr](https://github.com/MarvinJWendt/testza#CaptureStdoutAndStderr)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Fuzz-Utils">Fuzz Utils</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [FuzzUtilDistinctSet](https://github.com/MarvinJWendt/testza#FuzzUtilDistinctSet)
- [FuzzUtilLimitSet](https://github.com/MarvinJWendt/testza#FuzzUtilLimitSet)
- [FuzzUtilMergeSets](https://github.com/MarvinJWendt/testza#FuzzUtilMergeSets)
- [FuzzUtilModifySet](https://github.com/MarvinJWendt/testza#FuzzUtilModifySet)
- [FuzzUtilRunTests](https://github.com/MarvinJWendt/testza#FuzzUtilRunTests)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Fuzz-Booleans">Fuzz Booleans</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [FuzzBoolFull](https://github.com/MarvinJWendt/testza#FuzzBoolFull)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Fuzz-Strings">Fuzz Strings</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [FuzzStringEmailAddresses](https://github.com/MarvinJWendt/testza#FuzzStringEmailAddresses)
- [FuzzStringEmpty](https://github.com/MarvinJWendt/testza#FuzzStringEmpty)
- [FuzzStringFull](https://github.com/MarvinJWendt/testza#FuzzStringFull)
- [FuzzStringGenerateRandom](https://github.com/MarvinJWendt/testza#FuzzStringGenerateRandom)
- [FuzzStringHtmlTags](https://github.com/MarvinJWendt/testza#FuzzStringHtmlTags)
- [FuzzStringLong](https://github.com/MarvinJWendt/testza#FuzzStringLong)
- [FuzzStringNumeric](https://github.com/MarvinJWendt/testza#FuzzStringNumeric)
- [FuzzStringUsernames](https://github.com/MarvinJWendt/testza#FuzzStringUsernames)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Fuzz-Float64s">Fuzz Float64s</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [FuzzFloat64Full](https://github.com/MarvinJWendt/testza#FuzzFloat64Full)
- [FuzzFloat64GenerateRandomNegative](https://github.com/MarvinJWendt/testza#FuzzFloat64GenerateRandomNegative)
- [FuzzFloat64GenerateRandomPositive](https://github.com/MarvinJWendt/testza#FuzzFloat64GenerateRandomPositive)
- [FuzzFloat64GenerateRandomRange](https://github.com/MarvinJWendt/testza#FuzzFloat64GenerateRandomRange)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Fuzz-Integers">Fuzz Integers</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [FuzzIntFull](https://github.com/MarvinJWendt/testza#FuzzIntFull)
- [FuzzIntGenerateRandomNegative](https://github.com/MarvinJWendt/testza#FuzzIntGenerateRandomNegative)
- [FuzzIntGenerateRandomPositive](https://github.com/MarvinJWendt/testza#FuzzIntGenerateRandomPositive)
- [FuzzIntGenerateRandomRange](https://github.com/MarvinJWendt/testza#FuzzIntGenerateRandomRange)
</td>
</details>
</tr>
<tr>
<td><a href="https://github.com/MarvinJWendt/testza#Snapshot">Snapshot</a></td>
<td>
<details>
<summary>Click to expand</summary>
- [SnapshotCreate](https://github.com/MarvinJWendt/testza#SnapshotCreate)
- [SnapshotCreateOrValidate](https://github.com/MarvinJWendt/testza#SnapshotCreateOrValidate)
- [SnapshotValidate](https://github.com/MarvinJWendt/testza#SnapshotValidate)
</td>
</details>
</tr>
</table>
### Assert
#### AssertCompletesIn
```go
func AssertCompletesIn(t testRunner, duration time.Duration, f func(), msg ...any)
```
AssertCompletesIn asserts that a function completes in a given time.
Use this function to test that functions do not take too long to complete.
NOTE: Every system takes a different amount of time to complete a function.
Do not set the duration too low, if you want consistent results.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertCompletesIn(t, 2 * time.Second, func() {
// some code that should take less than 2 seconds...
}) // => PASS
#### AssertContains
```go
func AssertContains(t testRunner, object, element any, msg ...any)
```
AssertContains asserts that a string/list/array/slice/map contains the
specified element.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertContains(t, []int{1,2,3}, 2)
testza.AssertContains(t, []string{"Hello", "World"}, "World")
testza.AssertContains(t, "Hello, World!", "World")
#### AssertDecreasing
```go
func AssertDecreasing(t testRunner, object any, msg ...any)
```
AssertDecreasing asserts that the values in a slice are decreasing. the test
fails if the values are not in a slice or if the values are not comparable.
Valid input kinds are: []int, []int8, []int16, []int32, []int64, []uint,
[]uint8, []uint16, []uint32, []uint64, []float32, []float64.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertDecreasing(t, []int{1000, 137, 2, 1})
testza.AssertDecreasing(t, []float32{13.5, 7, 0.1, -10.3})
#### AssertDirEmpty
```go
func AssertDirEmpty(t testRunner, dir string, msg ...any)
```
AssertDirEmpty asserts that a directory is empty. The test will pass when
the directory is empty or does not exist.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertDirEmpty(t, "FolderName")
#### AssertDirExists
```go
func AssertDirExists(t testRunner, dir string, msg ...any)
```
AssertDirExists asserts that a directory exists. The test will pass when the
directory exists, and it's visible to the current user. The test will fail,
if the path points to a file.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertDirExists(t, "FolderName")
#### AssertDirNotEmpty
```go
func AssertDirNotEmpty(t testRunner, dir string, msg ...any)
```
AssertDirNotEmpty asserts that a directory is not empty The test will pass
when the directory is not empty and will fail if the directory does not
exist.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertDirNotEmpty(t, "FolderName")
#### AssertEqual
```go
func AssertEqual(t testRunner, expected any, actual any, msg ...any)
```
AssertEqual asserts that two objects are equal.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertEqual(t, "Hello, World!", "Hello, World!")
testza.AssertEqual(t, true, true)
#### AssertEqualValues
```go
func AssertEqualValues(t testRunner, expected any, actual any, msg ...any)
```
AssertEqualValues asserts that two objects have equal values. The order of
the values is also validated.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertEqualValues(t, []string{"Hello", "World"}, []string{"Hello", "World"})
testza.AssertEqualValues(t, []int{1,2}, []int{1,2})
testza.AssertEqualValues(t, []int{1,2}, []int{2,1}) // FAILS (wrong order)
Comparing struct values:
person1 := Person{
Name: "Marvin Wendt",
Age: 20,
Gender: "male",
}
person2 := Person{
Name: "Marvin Wendt",
Age: 20,
Gender: "male",
}
testza.AssertEqualValues(t, person1, person2)
#### AssertErrorIs
```go
func AssertErrorIs(t testRunner, err, target error, msg ...any)
```
AssertErrorIs asserts that target is inside the error chain of err.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
var testErr = errors.New("hello world")
var testErrWrapped = fmt.Errorf("test err: %w", testErr)
testza.AssertErrorIs(t, testErrWrapped ,testErr)
#### AssertFalse
```go
func AssertFalse(t testRunner, value any, msg ...any)
```
AssertFalse asserts that an expression or object resolves to false.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertFalse(t, false)
testza.AssertFalse(t, 1 == 2)
testza.AssertFalse(t, 2 != 2)
testza.AssertFalse(t, 1 > 5 && 4 < 0)
#### AssertFileExists
```go
func AssertFileExists(t testRunner, file string, msg ...any)
```
AssertFileExists asserts that a file exists.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertFileExists(t, "./test.txt")
testza.AssertFileExists(t, "./config.yaml", "the config file is missing")
#### AssertGreater
```go
func AssertGreater(t testRunner, object1, object2 any, msg ...any)
```
AssertGreater asserts that the first object is greater than the second.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertGreater(t, 5, 1)
testza.AssertGreater(t, 10, -10)
#### AssertGreaterOrEqual
```go
func AssertGreaterOrEqual(t testRunner, object1, object2 interface{}, msg ...interface{})
```
AssertGreaterOrEqual asserts that the first object is greater than or equalFrequently Asked Questions
What is testza?
testza is a Testing library for the Go programming language. Full-featured test framework with nice colorized output
How do I install testza?
Install testza with the Go module system using `go get MarvinJWendt/testza`. Check the repository for the current installation instructions.
What category does testza belong to?
testza is listed under Testing, specifically Testing Frameworks.