Skip to content
CCPEDIAby Unity Nodes
VideosLesson 1: Introduction to Testing in Daml

Lesson 1: Introduction to Testing in Daml

Nothing is loaded from YouTube until you press play, and then without tracking cookies.

About this video

This tutorial explores the tools and techniques available in Daml for testing code extensively. The focus is on Daml scripts, which allow us to define test scenarios that simulate parts of a business workflow, and run them against a ledger. The tutorial explains how to run scripts using Visual Studio Code as part of the Daml SDK, or using the command line interface or CLI. It also covers Daml test, a tool that runs all test scripts in the specified file or in all Daml files in the current project folder, and produces a test coverage report as the output on the console. The tutorial also explains how to use a Canton Sandbox to test scripts, and how to use Daml start to compile, start the Canton Ledger, upload the DAR, and run the test. Finally, the tutorial covers Navigator, a front-end app that can be used to connect to any Canton Ledger and modify the ledger.

For a better learning experience, sign up and enroll in this free course at: daml.talentlms.com/plus/catalog/courses/144

Transcript

1,352 words

Captions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.

  1. 0:01

    testing is integral to building a highquality software and daml offers many tools and techniques to make sure that you can test your code extensively so let's look at some of these tools and techniques to test daml templates let us start with the tools available in the daml SDK the first one is scripts we've already seen how scripts help us Define test scenarios that simulate parts of a business workflow when we run the scripts in the

  2. 0:30

    visual studio code as part of the daml SDK we're running them against a test Ledger or temporary Ledger we can also run scripts using the command line interface or CLI against a temporary ledger to run them against an actual Ledger we start a damel Sandbox and run the scripts in that sandbox daml sandbox also called The Canton sandbox or just sandbox is an instance of an inmemory can Ledger which is used for development environments let's look at each of these

  3. 1:05

    one by one we will start with a simple template bank account that has one choice deposit and use it for our test cases in the script written in the test scripts. daml we create two parties bank and Alice then we submit a command to create a bank account and then exercise the deposit choice on it given this set set up we can use the script view by simply running the script it gives us several table views with or without

  4. 1:37

    archived contracts and detailed disclosure it also gives us a detailed transaction treeview that shows the sequence of actions committed to The Ledger but if we want to run more than one script then we can run them from the command line daml test is a tool that runs all test scripts in the specified file or in in all daml files in the current project folder and produces a test coverage report as the output on the console as you can see here the test

  5. 2:09

    summary shows details such as number of active contracts transactions and test coverage metrics also note that the test coverage shows 50% for choices and that is because the second choice here is archive which is implicitly defined in all templates as our script does not test it the report indicates that it has not been tested you can have multiple files that have test scripts written in them if you want to run only one test file you can use the files option the

  6. 2:43

    next approach to run our scripts is to use a Canton sandbox which is a simple Ledger used for rapid prototyping by simulating a Canton Ledger locally we can use it for testing in two ways the first way has four steps starting starting with compiling the demo code into a Dar file then we start the Canton sandbox upload the DAR file and then run the script using the command as shown here this command is daml script followed by the name of the DAR the

  7. 3:15

    script the host and the port number since this option is running outside the IDE if we want to see some output from this test we need to use debug statements in the script to print the output on the console the second option is to use daml start which is also available as a tool from daml assistant at this point it is important to take a look at our dl. yaml file which comes from the predefined daml project template installed with daml SDK that we used as a starting

  8. 3:49

    point for our daml project dl. yaml is the project config file that must be in the root of your project directory as shown in the example example here it lists the current SDK version project folder name name of the initialization script version of the DAR files to be created and project dependencies the daml start tool uses the init script to initialize the sandbox creating an init script for initializing the sandbox isn't a topic

  9. 4:21

    we'll focus on at this point so for now if we try to build this main. daml as it has been written it will throw an error so let's comment it out for the error to go away now before we go any further let us revisit these two options in a bit more detail when we run a script against a ledger outside of the IDE we cannot see The Ledger in the transaction tree as we were able to do before instead we can write debug statements in the script

  10. 4:52

    itself to see some output on the console so here is our script with two debug statements both showing Alice's account before and after the deposit transaction now we can start the terminal and say damel build which compiles all daml code into Dar files notice that the version numbering of the DAR file depends on the version number provided in daml yaml then we start daml sandbox which will give us a message when the Canon sandbox is ready notice

  11. 5:26

    that if this goes well the Canton sandbox is ready and listening at a default Port 6865 you can make it run on a different port number by specifying a port number and make sure that this port is free otherwise you will see an error message indicating that the port is already being used at this point you need to leave this terminal running and open another terminal where you will say daml Ledger upload Dar followed by the DAR

  12. 5:55

    file name since there is only one Dar at this point it will will be uploaded and you can see a message that the DAR upload has succeeded finally we can run the script by saying daml script Dar followed by the full path name of the DAR file from the rout script name test scripts test Bank service Ledger host Local Host Ledger Port 6865 this will run the script on The Ledger and print the debug statements on

  13. 6:26

    the console the second option of running scripts on a Canton sandbox as we discussed is pretty straightforward we simply say daml start this will compile the code start the sandbox and upload the D once it is done you can now open another terminal to run the script as shown before now when you do daml start you would also notice that it starts the navigator app on the port 7500 Navigator is a front-end app that can be used to connect to any Canton

  14. 7:01

    Ledger and modify The Ledger we use it during daml development as yet another tool to explore the flow and implications of the daml models it comes bundled with the daml SDK when you run daml start you will see it in your browser as it will open up a new tab and display a login page with a choose your roll dropdown however there won't be anything in the drop-down because we did not set up anything in our init script but we'll get to that soon enough let's review

  15. 7:34

    what we covered in this lesson in this lesson we looked at daml scripts and how they can be used to conduct extensive testing of the code written in daml templates these scripts can be run in different ways if we want to test at a script level we run the script from within the IDE to perform unit testing which is the most granular level of script execution we can run the scripts from a command line interface or CLI using daml

  16. 8:05

    assistance tool that is daml test this option allows us to run scripts at a file level we can run all the scripts in a file or all scripts within all the files in a project if we want to run our scripts against a Canton sandbox environment then daml offers two ways to do that first using a set of tools in a sequence Dam build daml sandbox daml upload Dar and then daml script and

  17. 8:35

    second using daml start followed by daml Script