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

#[test_script] for unit testing #1832

Closed
5 of 8 tasks
adlerjohn opened this issue Jun 2, 2022 · 4 comments · Fixed by #2985
Closed
5 of 8 tasks

#[test_script] for unit testing #1832

adlerjohn opened this issue Jun 2, 2022 · 4 comments · Fixed by #2985
Assignees
Labels
compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request P: high Should be looked at if there are no critical issues left testing General testing

Comments

@adlerjohn
Copy link
Contributor

adlerjohn commented Jun 2, 2022

Add a #[test_script] that defines a script with no input parameters and an optional output value. Only valid for contracts. The compiler must:

  1. compile the contract entirely, then
  2. compile each script test independently, allowing implicit use of any ABI declarations the contract implements, then
  3. output each script test's compiled output to a different .bin file.

This will be sufficient for downstream tools such as forc test to pick them up and automatically run them, as a first step to in-language testing.


Following added by @mitchmindtree.

TODO

  • Forward attributes from ParseProgram to TyProgram #2979.
  • Add #[test] to list of known attributes in sway-core #2980.
    • Support the #[test] attribute on Sway functions.
    • Add #[test] to set of known attributes.
    • Update original compile function (i.e. forc build) to ignore all #[test] functions. Likely best done during convert_parse_tree.
  • Update sway-core to provide an alternative to compile called compile_tests that lazily generates bytecode for each test function, treating each as an entry point.
    1. Type check full program (including #[test] functions).
    2. Add #[test] functions as entry points for dead code analysis.
    3. Generate bytecode for each #[test] function entrypoint.
  • Add a forc test command that executes the bytecode.
  • Add a forc check --tests flag for use by sway-lsp to type-check all tests (without codegen or running them).

Follow-up

  • Refactor E2E tests to take advantage of new #[test] feature where possible.
  • Consider moving some std lib unit tests back to std library and use #[test], rather than in E2E tests?
  • Introduce [dev-dependencies] table to Forc.toml.
@adlerjohn adlerjohn added enhancement New feature or request compiler General compiler. Should eventually become more specific as the issue is triaged P: high Should be looked at if there are no critical issues left labels Jun 2, 2022
@adlerjohn adlerjohn moved this to Todo in Fuel Network Jun 2, 2022
@adlerjohn adlerjohn added the good first issue Good for newcomers label Jun 2, 2022
@mitchmindtree mitchmindtree self-assigned this Sep 8, 2022
mitchmindtree added a commit that referenced this issue Oct 10, 2022
…ation of unit testing support (#2264)

This PR is a WIP that aims to address the first 3 steps in #1833.

This is in anticipation of using `forc test` to support unit testing
(rather than Rust integration testing) #1832.

The `forc test` command remains for now, but outputs a message
explaining that the command is now reserved for unit testing and links
to the issues above.

## TODO

- [x] Create a new `sway-test-rs` repo or similar that can be used as a
`cargo generate` template.
- [x] Update Rust integration testing docs in the Sway book to describe
how to use the `cargo generate` command to easily add Sway integration
testing to an existing Rust project.

## Follow-up

- Create a `forc-test-rs` crate that re-exports and extends `fuels` with
useful `forc` functionality for integration testing (e.g. re-exporting
`forc_pkg::build` to ensure sway code is built and available under
`out/` at the start of testing).
@mitchmindtree
Copy link
Contributor

@adlerjohn are you happy to go with #[test] for this over #[test_script]? I don't recall if there was a particular motivation for #[test_script] instead 🤔

@adlerjohn
Copy link
Contributor Author

The motivation is that there needs to be a way to write tests that involve running a script transaction and calling one or more ABI methods, and a way to run a unit test that just happens without having to go through a transaction.

@mitchmindtree
Copy link
Contributor

Ahhh yes, perhaps we could go with something like this?

  • #[test] - base test declaration, "just happens".
  • #[test(script)] - indicates a script transaction is required in order to call one or more ABI methods.

This way we only need to check for test throughout the compiler when doing things like checking for extra entry points, testing for known attribute names, etc. Then we can just do something like .contains("script") on the inner set to determine if a transaction is required.

@adlerjohn
Copy link
Contributor Author

Interesting alternative. I'm impartial either way, so long as there's the two options. Might be a good time to get some bikeshedding comments in!

mitchmindtree added a commit that referenced this issue Oct 10, 2022
First steps of #1832, this adds basic awareness to the compiler for the
new `#[test]` attribute.

For now, we omit test functions when constructing the `ParseTree` during
regular compilation (i.e. `forc build` or `forc check`) so that:

1. Programs may continue to compile even if tests do not typecheck (like
Rust).
2. We don't do extra work type-checking test functions when we don't
have to.
3. We don't get a bunch of dead code warnings for `#[test]` fns.

Later on, I'll add an option to enable these during `forc test`, `forc
check --tests`, etc.

This PR doesn't enable compilation or execution of test functions just
yet. This will be addressed in a following PR (see #1832).

Adds a basic test that makes sure we don't provide any undesired
warnings about unknown attributes or dead code.
Repository owner moved this from Todo to Done in Fuel Network Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request P: high Should be looked at if there are no critical issues left testing General testing
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants