Skip to main content

Wasmite: Webassembly is the future but now it has a testing toolchain

Project description

What is the Wasmite project

Since WebAssembly is the future of the web. I decide to create Wasmite, a python package for unit-testing your wasm or wat code. Wasmite is based on wasmer and the python standard library package unittest. Documentation for can be found here: documentation for unittest and documentation for wasmer

This project was formerly an extension of my Rust/Python Web framework Wasp, so some section of the code may refer to it's earlier name (Native)

Wasmite looks for tests in python files whose names start with test_*.py and runs every test_* function it discovers. The testing folder has more examples.

Having any problems or questions create a issue, i will be happy to help :)

Installation

This project requires python 3 and doesn't support 3.9

pip install wasmite

Project Goals:

  • Import wasm or wat module successfully
  • Access functions within module
  • Type checking of parameters and the result of functions
  • Release to PyPi for public to use
  • Allow Wasmite ...
    • Export Python functions
    • Export Global Instances
    • Export Memory Instances
  • More complex examples in testing folder
  • Receive community on how to improve

Examples:

from wasmite import WasmiteCase, WasmModule
from wasmite import FunctionTypes, Function, Global, Value, main
from wasmite import I32

def sum(x: int, y: int) -> int:
    """ python function to be imported into WASM  """
    return x + y

class Test(WasmiteCase):
    # create a variable the hold all the functions from a specific wasm file.
    module = WasmModule("test_wasm.wasm")
    # import python function into WASM 
    # type annotations on the function is necessary 
    module.register("math", {
        "sum": Function(module.store, sum),
        "seven": Global(module.store, Value.i32(7), mutable=True)
    })
    # start up the module and return the exports (this is mandatory)
    exports = module.get_exports()

    def test_add(self):
        # test add function
        result = self.exports.add(1,2)
        self.assertEqual(result, 3) 

    def test_sub(self):
        # test the sub function
        result = self.exports.sub(2,2)
        self.assertEqual(result, 0)

    def test_args_add(self):
        # check the types for results and parameter of the function "add"
        # param is I32, I32 and result is I32
        add_function = self.exports.add
        self.assertTypes(add_function, FunctionTypes([I32, I32], [I32])) # result will fail

    def test_import_sum(self):
        # test the imported python function sum.
        sum_function = self.exports.addsum(5,2)
        self.assertEqual(sum_function, 7)

    def test_global_read(self):
        # test reading value of global
        read_seven = self.exports.read_global()
        self.assertEqual(read_seven, 7) 

    def test_global_write(self):
        # test writing value of global
        self.exports.write_global(5)
        read_seven = self.exports.read_global()
        self.assertEqual(read_seven, 5) 

# Hi don't forget to add me         
if __name__ == "__main__":
    main()

-->

Then you can then run this test like so:

# make sure you are in examples/wasm
$ python test_wasm.py

test_add (__main__.Test) ... ok
test_args_add (__main__.Test) ... ok
test_global_read (__main__.Test) ... ok
test_global_write (__main__.Test) ... ok
test_import_sum (__main__.Test) ... ok
test_sub (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.001s

OK

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wasmite-0.2.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wasmite-0.2.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file wasmite-0.2.1.tar.gz.

File metadata

  • Download URL: wasmite-0.2.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.8

File hashes

Hashes for wasmite-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1da8dad64a77a7b078d1249f34ed3d153cad07e164afa1fa230b240307b87a65
MD5 7025f3dbea163c2813f1c36832f76a50
BLAKE2b-256 3dbe7f87f0ea39f48aa5c20298c1cb80b89092543240eac63224c55720522488

See more details on using hashes here.

File details

Details for the file wasmite-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: wasmite-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.8

File hashes

Hashes for wasmite-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 edae9a2a8baa5772c848848f88597644d80ccd558bd04d30522ba1ca109a7e7c
MD5 ec611b16dcc3cf08c426130cd330d89e
BLAKE2b-256 cb49fcaf909bd96372da9d679900ccaa45a333d5d67c2da181bed5d55c736935

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page