How to Create Proper Python Unit Test for DAZL

Hello all,
I’m attempting to write a correct test for our python dazl client.
It seems Testing and dazl-client/unittest_example.py at 63362f4a4c257b012a5832be847f257f89b55731 · digital-asset/dazl-client · GitHub use the old way of dazl instead of the dazl.ledger module. dazl.ledger

Can I ask you what the recommended way to setup the tests are? We especially want to make sure they work properly with github actions so we can add them as presubmits to our PRs. Thank you.

Thanks for asking! The documentation here would be the correct place to point you to, but unfortunately I see these are also out of date as they also used the old APIs instead of the new ones.

The best example of a test in the new API is tests/unit/test_ledger_query.py, which makes use of a pytest fixture tests/conftest.py to provide a sandbox that is left running for the duration of the test run. Note that many dazl tests run against Daml V1 and Daml V2 sandboxes to ensure backwards compatibility, but chances are you don’t need that aded complexity.

So to put it all together, and assuming you’re using you’re using pytest:

import dazl

@pytest.fixture(scope="session")
def sandbox():
    # replace project_root with the location of your daml.yaml file,
    # and/or specify the version of Daml SDK to use
    with dazl.testing.sandbox(project_root=None, version="2.0.0") as sb:
        yield sb.url

@pytest.mark.asyncio
async def test_that_uses_sandbox(sandbox):
    async with connect(url=sandbox, admin=True) as conn:
         # set up parties
         p = await conn.allocate_party()
    async with connect(url=sandbox, act_as=p.party) as conn:
        # do stuff with conn
        ...

Make sure to keep the result of allocate_party handy, especially when using Daml 2. Also make sure that your CI has both Python and Java available, as all versions of the sandbox require a JVM to run.

Thank you very much! I’ll try to run with the given examples!

Edit3: Currently we have a test that runs locally ok. (I manually executed the lines in Setup.daml as conn.upload_package does not seem to execute the init-script.) However, when I try to run it in github as a presubmit action, it runs into an error like this:

    with testing.sandbox(project_root=None, version='2.0.0') as sb:
  File "BAZEL_DIRECTORYSTUFF/test.runfiles/pip_pypi__dazl/dazl/testing/_sandbox.py", line 126, in __enter__
    self.start()
  File "BAZEL_DIRECTORYSTUFF/test.runfiles/pip_pypi__dazl/dazl/testing/_sandbox.py", line 94, in start
    self._process = subprocess.Popen(
  File "BAZEL_DIRECTORYSTUFF/external/hermetic_python_install/bazel_install/python-3.8.13/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "BAZEL_DIRECTORYSTUFF/hermetic_python_install/bazel_install/python-3.8.13/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'daml'

Is there a recommended way to set it up properly so that it would succeed?

Housekeeping note: this follow up question is posted on a new thread and is being answered there.