How best to test a Python-based snap

I’m writing a snap which is based on a Python module, and I’m at the stage where I’m thinking about testing it, in a way that can be run in a Continuous Integration setup.

I can easily test the Python code in the normal way (using unittest, pytest etc.), which can check that the code logically works in isolation.

However, about half of the issues I’ve encountered while developing relate to the snap confinement - access to specific parts of the filesystem etc.

Does Snapcraft provide anything to help with testing my app with the confinement settings and plugs defined in the snapcraft.yaml? Or are there standard techniques to automate building the snap and running commands within it in an automated, scriptable way?

We, for snapcraft itself, use Travis CI to test the snap we built as part of any PR.

An ideal workflow that we are working on is to have a webhook (or something of the sort) from build.snapcraft.io trigger a new build on Travis CI using the a revision from the edge channel to mark that revision as tested.

1 Like

Thanks @sergiusens

I was hoping for a way to test the snap without having to push it to the charmstore first. Is that the sort of thing you do in Travis for snapcraft? Where would I find the code for that? I tried looking in .travis.yaml and runtests.sh but it wasn’t immediately obvious to me.

Ah I found https://github.com/snapcore/snapcraft/blob/master/tests/spread/general/build_environment/task.yaml, where you appear to just do:

output="$(snapcraft)"
...
sudo snap install build-environment-test_*.snap --dangerous

I’ll play around with doing something similar then.

It is in our travis.yaml,

What is interesting about the snapcraft tests is that we need to verify that the snap of snapcraft we create can still properly create snaps that run, which is why you see those in our test suites :slight_smile: