Repository: MarvinJWendt/testza
Pull request #61 (2021-11-02 00:20:40)

feat(configuration): added `SetRandomSeed`


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58f4cac..574272f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 
 ### Features
 - **assert:** added AssertNoDirExists and AssertDirExists. fixes [#36](https://github.com/MarvinJWendt/testza/issues/36)
+- **configuration:** added `SetRandomSeed`
 
 
 <a name="v0.2.10"></a>
diff --git a/README.md b/README.md
index 75e417c..bf9be0b 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@
 </a>
 
 <a href="https://codecov.io/gh/MarvinJWendt/testza">
-<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-2744-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
+<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-2763-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
 </a>
   
 <a href="https://pkg.go.dev/github.com/MarvinJWendt/testza" target="_blank">
@@ -113,6 +113,7 @@ testza.MockStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s
 
   - [SetColorsEnabled](https://github.com/MarvinJWendt/testza#SetColorsEnabled)
   - [SetLineNumbersEnabled](https://github.com/MarvinJWendt/testza#SetLineNumbersEnabled)
+  - [SetRandomSeed](https://github.com/MarvinJWendt/testza#SetRandomSeed)
 </td>
 
 </details>
@@ -1285,6 +1286,24 @@ Example:
       testza.SetLineNumbersEnabled(true)  // Enable line numbers
     }
 
+#### SetRandomSeed
+
+```go
+func SetRandomSeed(seed int64)
+```
+
+SetRandomSeed sets the seed for the random generator used in testza. Using
+the same seed will result in the same random sequences each time and
+guarantee a reproducible test run. Use this setting, if you want a 100%
+deterministic test. You should use this in the init() method of the package,
+which contains your tests.
+
+Example:
+
+    init() {
+      testza.SetRandomSeed(1337) // Set the seed to 1337
+    }
+
 ### Snapshot
 
 #### SnapshotCreate
diff --git a/configuration.go b/configuration.go
index cdaec42..d9f04cf 100644
--- a/configuration.go
+++ b/configuration.go
@@ -1,6 +1,8 @@
 package testza
 
 import (
+	"math/rand"
+
 	"github.com/MarvinJWendt/testza/internal"
 	"github.com/pterm/pterm"
 )
@@ -32,3 +34,18 @@ func SetColorsEnabled(enabled bool) {
 func SetLineNumbersEnabled(enabled bool) {
 	internal.LineNumbersEnabled = enabled
 }
+
+// SetRandomSeed sets the seed for the random generator used in testza.
+// Using the same seed will result in the same random sequences each time and guarantee a reproducible test run.
+// Use this setting, if you want a 100% deterministic test.
+// You should use this in the init() method of the package, which contains your tests.
+//
+// Example:
+//  init() {
+//    testza.SetRandomSeed(1337) // Set the seed to 1337
+//    testza.SetRandomSeed(time.Now().UnixNano()) // Set the seed back to the current time (default | non-deterministic)
+//  }
+func SetRandomSeed(seed int64) {
+	randomSeed = seed
+	rand.Seed(seed)
+}


Test patch:
diff --git a/configuration_test.go b/configuration_test.go
index d4ae71d..e094245 100644
--- a/configuration_test.go
+++ b/configuration_test.go
@@ -2,6 +2,7 @@ package testza
 
 import (
 	"testing"
+	"time"
 
 	"github.com/MarvinJWendt/testza/internal"
 	"github.com/pterm/pterm"
@@ -30,3 +31,12 @@ func TestSetLineNumbersEnabled(t *testing.T) {
 		AssertTrue(t, internal.LineNumbersEnabled)
 	})
 }
+
+func TestSetRandomSeed(t *testing.T) {
+	SetRandomSeed(1337)
+	AssertEqual(t, int64(1337), randomSeed)
+	AssertEqual(t, "4U390O49B9", MockInputStringGenerateRandom(1, 10)[0])
+	un := time.Now().UnixNano()
+	SetRandomSeed(un)
+	AssertEqual(t, un, randomSeed)
+}
diff --git a/testza.go b/testza.go
index 8da4929..27c4583 100644
--- a/testza.go
+++ b/testza.go
@@ -5,6 +5,9 @@ import (
 	"time"
 )
 
+var randomSeed int64
+
 func init() {
-	rand.Seed(time.Now().UnixNano())
+	randomSeed = time.Now().UnixNano()
+	rand.Seed(randomSeed)
 }