Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow writing unit tests for WASM applications #341

Merged
merged 24 commits into from
Jan 17, 2023
Merged

Allow writing unit tests for WASM applications #341

merged 24 commits into from
Jan 17, 2023

Conversation

jvff
Copy link
Contributor

@jvff jvff commented Jan 16, 2023

Motivation

Developers writing applications for Linera should be able to write unit tests with minimal divergence from their usual workflow.

Solution

Based on the ideas from near/near-sdk-rs#467 (more specifically, the webassembly-test and webassembly-test-runner crates discussed here), a custom test runner in linera-sdk was created that is able to execute specially annotated unit tests in WASM application modules.

To verify this works, some unit tests were written for the Counter example application. The "wasm" CI job was updated to run those tests.

Related issues

Closes #272

jvff added 3 commits January 16, 2023 19:38
Use the test runner binary source code from the
`webassembly-test-runner` crate version 0.1.0.
Provide a brief description of its purpose and how to use it.
And rename variables to make clearer their purpose.
@jvff jvff added the testing label Jan 16, 2023
@jvff jvff added this to the Local testnet milestone Jan 16, 2023
@jvff jvff requested a review from ma2bd January 16, 2023 19:46
@jvff jvff self-assigned this Jan 16, 2023
jvff added 13 commits January 16, 2023 20:06
Separate loading the WebAssembly test module.
Simplify so that the loop only returns `TestMeta` as items.
Simplify the name a little.
Move code to parse test information from exported function name into the
constructor.
Use `tests.len()` to be clearer.
Replace loop with iterators for conciseness.
Collect all test report handling code in a new type.
Ensure CI fails if any of the tests executed fail.
Reset the runtime environment after every test.
Move code related to running a test into the new method.
A helper method that updates the report based on the result of a
test function execution.
Prepare to be able to add exported APIs to the test environment.
Allow executing the test module even if the imports it depends on aren't
available. Tests that try to use those imports will fail.
Copy link
Contributor

@ma2bd ma2bd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Let's land this to unblock everyone (@MathieuDutSik ?) and iterate later if needed.

linera-sdk/src/lib.rs Outdated Show resolved Hide resolved
jvff added 8 commits January 17, 2023 13:21
Make it easier for tests to compare instances of those types.
Check that it returns the serialized value in the application's state.
Pass some garbage data as the query parameter and check that it results
in an error.
Application should initialize correctly with a provided initial value
and increment it after executing an operation.
Check that an error is returned if an effect is requested to be
executed.
Should treat it exactly like an operation.
An error should be returned because the application doesn't support
sessions.
Use the custom test runner in `linera-sdk` to run the unit tests.
@jvff jvff merged commit f6f8f44 into linera-io:main Jan 17, 2023
@jvff jvff deleted the counter-unit-tests branch January 17, 2023 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add framework for writing unit tests
2 participants