• 官网
  • 中文
  • English
  • Russian
  • More language
Show / Hide Table of Contents
  • NEO Basics
    • About NEO
    • NEO Whitepaper
    • NEO Contract Whitepaper
    • Consensus
      • Consensus Whitepaper
      • Consensus Diagram
  • NEO Node
    • Introduction
    • NEO-GUI
      • Installing the Client
      • Wallet
      • Transactions
      • Registering and Distributing Assets
      • Interacting with Smart Contracts
      • Election and Voting
      • Other Features
    • NEO-CLI
      • Publishing from source
      • Installation
      • SeedList
      • NEO-CLI Command line
      • API Reference
        • v2.9.0 and later
          • dumpprivkey
          • getaccountstate
          • getassetstate
          • getbalance
          • getbestblockhash
          • getblock
          • getblock
          • getblockcount
          • getblockhash
          • getblockheader
          • getblocksysfee
          • getconnectioncount
          • getcontractstate
          • getnewaddress
          • getrawmempool
          • getrawtransaction
          • getstorage
          • gettxout
          • getpeers
          • getvalidators
          • getversion
          • getwalletheight
          • invoke
          • invokefunction
          • invokescript
          • listaddress
          • sendfrom
          • sendrawtransaction
          • sendtoaddress
          • sendmany
          • validateaddress
        • v2.8.0
          • dumpprivkey
          • getaccountstate
          • getapplicationlog
          • getassetstate
          • getbalance
          • getbestblockhash
          • getblock
          • getblock
          • getblockcount
          • getblockhash
          • getblocksysfee
          • getconnectioncount
          • getcontractstate
          • getnewaddress
          • getrawmempool
          • getrawtransaction
          • getstorage
          • gettxout
          • getpeers
          • getvalidators
          • getversion
          • invoke
          • invokefunction
          • invokescript
          • listaddress
          • sendfrom
          • sendrawtransaction
          • sendtoaddress
          • sendmany
          • validateaddress
        • v2.7.6
          • dumpprivkey
          • getaccountstate
          • getapplicationlog
          • getassetstate
          • getbalance
          • getbestblockhash
          • getblock
          • getblock
          • getblockcount
          • getblockhash
          • getblocksysfee
          • getconnectioncount
          • getcontractstate
          • getnewaddress
          • getrawmempool
          • getrawtransaction
          • getstorage
          • gettxout
          • getpeers
          • getvalidators
          • getversion
          • invoke
          • invokefunction
          • invokescript
          • listaddress
          • sendfrom
          • sendrawtransaction
          • sendtoaddress
          • sendmany
          • validateaddress
    • NEO Plug-ins
    • NEO-Python
  • NEO Network
    • Test Network
    • Private Chain
      • Build a private chain
      • Build a private chain in an easy way
    • Local Blockchain
    • Synchronizing the blockchain faster
    • Network Protocol
  • NEO Utilities
    • NEO SDK
      • About NEO SDK
      • Common Uses
      • Account
      • Wallet
      • Transaction Construction
    • Blockchain Explorers
  • Smart Contract
    • Introduction
    • Quick Start
      • Getting Started
        • Using C# (Windows)
        • Using C# (macOS)
        • Using C# (ubuntu)
        • Using Java
        • Using Python
      • Limitations
      • Unit Testing
      • Deploying and Invoking Smart Contracts
    • Reference
      • API Reference
        • NEO
        • System
      • Framework
        • .NET Framework
          • NEO
            • Account
            • Asset
            • Block
            • Blockchain
            • Contract
            • Enrollment
            • Header
            • Runtime
            • Storage
            • StorageContext
            • Transaction
            • TransactionAttribute
            • TransactionInput
            • TransactionOutput
            • TriggerType
            • Validator
          • System
            • ExecutionEngine
    • Samples and Tutorials
      • Hello World
      • Lock Contract
      • Domain (Domain Name System)
      • Contract Authentication Tutorial
      • Deploying a Lock Contract
    • Parameters and Return Values
    • Contract Triggers
    • System Fees
  • FAQ
  • Document for Exchange Developers
    • v2.9.4
    • v2.9.3
    • v2.9.2
    • v2.9.1
    • v2.9.0
    • v2.8.0
    • v2.7.6

Unit Testing

You can use the invokeScript method provided by NEO-CLI for unit testing.

Testing tool

A unit test is initiated by a POST request. This section we use a common POST tool POSTMAN to do the testing. You can write a POST tool for testing as well.

After compiling your smart contract and getting the related script hash, you can use POSTMAN to test by following these steps:

  1. Configure the CLI address. Select POST in the list and enter the RPC interface address of NEO-CLI.

  2. Select raw and JSON, and configure as follows:

    {
     "jsonrpc":"2.0",
     "method":"invokescript",
     "params":["03230fa0"],
     "id":1
    }
    

    Replace the string in params with the script hash of the smart contract to test. Click Send to start a testing. See the figure below:

    img

Example 1 - Testing a contract without parameters

  1. Write the following code and save the generated .avm file as d:\\1.avm.

    public class Test01 : SmartContract
    {
        public static object Main()
        {
            var magicstr = "Hello, World!";
            return magicstr;
        }
    }
    
  2. Create an netcore project and import the NEO project.

    img

    Write the following code to obtain the smart contract script hash:

    class Program
        {
            static void Main(string[] args)
            {
                var noparamAVM = System.IO.File.ReadAllBytes("d:\\1.avm");
                var str = Neo.Helper.ToHexString(noparamAVM);
                Console.WriteLine("AVM=" + str);
                Console.ReadLine();
            }
    }
    

    After running above code you can get the contract script: “52c56b6c766b00527ac461516c766b51527ac46203006c766b51c3616c7566”.

  3. Use postman to test, as shown below:

    img

"state": The value "HALT, BREAK" indicates the test is successful.

Stack is the value left on the Stack, which is the bytearray corresponding to the string "Hello, World!".

Example 2 - Testing a contract with parameters

  1. Write the following code and save the generated .avm file as d:\\2.avm。

    public class Test01 : SmartContract
    {
        public static object Main(string param1,int[] value)
        {
              var magicstr = "2018 02 21";
              return value[0]+value[1];
        }
    }
    
  2. Write the following code for testing:

     static void Main(string[] args)
            {
                var noparamAVM = System.IO.File.ReadAllBytes("d:\\2.avm");
                var str = Neo.Helper.ToHexString(noparamAVM);
    
                Neo.VM.ScriptBuilder sb = new Neo.VM.ScriptBuilder();
                sb.EmitPush(12);
                sb.EmitPush(14);
                sb.EmitPush(2);
                sb.Emit(Neo.VM.OpCode.PACK);
                sb.EmitPush("param1");
                var _params = sb.ToArray();
                var str2 = Neo.Helper.ToHexString(_params);
    
                Console.WriteLine("AVM=" + str2 + str);
                Console.ReadLine();
            }
    
  3. Use PostMan to test:

    img

Example 3 - Testing a contract deployed on the blockchain

Majority of steps are similar. Refer to the first two examples.

The following is the code for testing:

static void Main(string[] args)
{
     //var noparamAVM = System.IO.File.ReadAllBytes("d:\\2.avm");
     //var str = Neo.Helper.ToHexString(noparamAVM);
     Neo.VM.ScriptBuilder sb = new Neo.VM.ScriptBuilder();
     sb.EmitPush(12);
     sb.EmitPush(14);
     sb.EmitPush(2);
     sb.Emit(Neo.VM.OpCode.PACK);
     sb.EmitPush("param1");
     //To call the deployed contract, add EmitAppCall at the end
     var addr = Neo.UInt160.Parse("0x10ad2338f972e90406fd2ebea9a60f38f4aebd53");
     sb.EmitAppCall(addr.ToArray());
     var _params = sb.ToArray();
     var str2 = Neo.Helper.ToHexString(_params);
     Console.WriteLine("AVM=" + str2);
     Console.ReadLine();
}
  • Improve this Doc
Back to top Copyright © 2014-2018 NEO