github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/docs/concepts/gno-test.md (about) 1 --- 2 id: gno-test 3 --- 4 5 # Gno Test 6 7 There are two methods for testing a realm or package during the development phase: 8 9 1. Calling the realm/package after deploying it on a local network (or testnet). 10 2. Using the `test` option within the [`gno`](../gno-tooling/cli/gno.md) CLI. 11 12 While the first method is recommended for its accuracy and similarity to the actual deployment environment, it is more efficient to initially utilize the second method for composing test cases and then proceed to the first method if no errors are detected. 13 14 This section will teach you how to use the second method. 15 16 Writing test cases in Gno is similar to that of Go, with general rules as the following: 17 18 * Test file naming conventions must be adhered to (ex: `xxx_test.gno`). 19 * Test functions must start with `Test`. 20 * The `t *testing.T` argument must be included in each test function. 21 * The `testing` package must be imported. 22 * Tests must be run with the `gno test` command. 23 24 Let's write a sample code and test it. 25 26 ```go 27 // contract.gno 28 29 package demo 30 31 func Hello(name string) string { 32 return "Hello " + name + "!" 33 } 34 ``` 35 36 This is a simple code that returns the string-typed argument in a specific format. 37 38 Next, we'll write a test case that looks like the following: 39 40 ```go 41 // contract_test.gno 42 43 package demo 44 45 import "testing" 46 47 func TestHello(t *testing.T) { 48 { 49 got := Hello("People") 50 expected := "Hello People!" 51 if got != expected { 52 t.Fatalf("expected %q, got %q.", expected, got) 53 } 54 } 55 { 56 got := Hello("") 57 expected := "Hello People!" 58 if got != expected { 59 t.Fatalf("expected %q, got %q.", expected, got) 60 } 61 } 62 } 63 ``` 64 65 Two conditions exist in the test case above. 66 67 1. "Hello People!" should be returned when calling `Hello("People")`. 68 2. "Hello People!" should be returned when calling `Hello("")`. 69 70 Upon examination of our realm code and the associated test results, the initial condition exhibited the desired behavior; however, an error was identified in the second condition. 71 Despite the expected outcome of "Hello" being returned, the test case incorrectly specified that the expected output should be "Hello People!" instead. 72 73 Replacing the second test case with the following will successfully fix the issue and allow the test to pass. 74 75 ```go 76 { 77 got := Hello("") 78 expected := "Hello !" 79 if expected != got { 80 t.Fatalf("expected %q, got %q.", expected, got) 81 } 82 } 83 ``` 84 85 ## Blockchain context in tests 86 Running `gno test` executes files within the directory that end with `_test.gno` and `_filetest.gno`. 87 Internally, a GnoVM instance is initialized to run the test, and, at that moment, 88 a blockchain-related context is injected into the GnoVM. Utilizing this context, the transaction sender, 89 coins, block height, etc. can be mocked. 90 91 For detailed information on these functions, refer to their [reference page](../reference/stdlibs/std/testing.md).