Use dubhe to test a contract

We’ll start by creating a test file.

touch contracts/counter/sources/tests/message.move

deploy_dapp_for_testing is the test method that Dubhe automatically generates for you. Let’s write the test.

#[test_only]
module dms::message_test {
   use sui::test_scenario;
   use std::ascii::string;
   use dms::message_system;
   use dms::message_schema::Message;
   use dms::init_test;
   #[test]
   public fun send() {
       let (mut scenario, dapp)  = init_test::deploy_dapp_for_testing(@0xA);
       let mut schema = test_scenario::take_shared<Schema>(&scenario);
       assert!(schema.message().get() == string(b"Hello, World!"));
 
       let ctx = test_scenario::ctx(&mut scenario);
       message_system::send(&mut schema, string(b"Move, Move!"), ctx);
       assert!(schema.message().get() == string(b"Move, Move!"));
 
       test_scenario::return_shared(schema);
       dapp.distroy_dapp_for_testing();
       scenario.end();
   }
}

Let’s run the Test command to check for syntax errors.

  dubhe-template-project pnpm dubhe test
🚀 Running move test
INCLUDING DEPENDENCY Dubhe
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING dms
Running Move unit tests
[ PASS    ] dms::message_test::send
Test result: OK. Total tests: 1; passed: 1; failed: 0
Total number of linter warnings suppressed: 1 (unique lints: 1)

Run a subset of tests or list them

To run only tests whose fully qualified name (address::module::function) contains a substring, pass it as a positional argument (same rules as sui move test [filter]):

pnpm dubhe test message_test::send
pnpm dubhe test send

The same filter can be passed with --test instead of the positional:

pnpm dubhe test --test message_test::send

To print all test names without running them:

pnpm dubhe test --list

See the CLI reference for all dubhe test options.