How to test a sway library other than creating a test contract reference to the library?

How to test a sway library other than creating a test contract reference to the library?

4 Likes

Hi @felix200912 , great question! Sway has in-language testing you can use for this. You can check out how that works in the Sway docs here: Unit Testing - The Sway Programming Language

1 Like

This is what I am looking for! Just to clarify, this unit testing can only be testing functions with no storage access (or basically no blockchain state access) right?

Are you referring to checking the state of a node or testing a function with the storage annotation?

1 Like

From the Sway book:

Unit tests decorated with the #[test(script)] attribute that are declared within contract projects may also call directly into their associated contract’s ABI.

I’ve not tested this feature myself yet, though I do use sway unit testing extensively when developing libraries.
Ref:forc test - The Sway Programming Language

2 Likes

Yes I was referring to testing functions that have storage access, like the ones with #[storage(read,write)]

1 Like

Thanks for clarifying! Yes, you can test functions with storage access.