Skip to content

feat: Add Serverpod test tools - #2713

Merged
hampuslavin merged 23 commits into
serverpod:mainfrom
hampuslavin:hl_test_framework_with_serverpod
Sep 17, 2024
Merged

hampuslavin merged 23 commits into
serverpod:mainfrom
hampuslavin:hl_test_framework_with_serverpod

Conversation

@hampuslavin

@hampuslavin hampuslavin commented Sep 5, 2024 •

Copy link
Copy Markdown
Contributor

Description

Introduces the "alpha version" of the new Serverpod test tools, as outlined on a high level here (specifically item 2 and 3 on the roadmap at the bottom).

The main feature is the withServerpod helper:

withServerpod(
  (endpoints, session) {
    test(
        'when not authenticated and calling returnsString then throws UnauthenticatedEndpointCallTestException',
        () async {
      session = session.copyWith(authentication: AuthenticationOverride.authenticationInfo(1234, {}));

      final result =
          endpoints.authenticatedTestTools.returnsString(session, "Hello");
      await expectLater(
          result, throwsA(isA<UnauthenticatedEndpointCallTestException>()));
    });
  // ...
  • Provides an endoints object that enables the user to easily test all their endpoints
  • Provides a test specific session object that lets the user call endpoints and the database just like they are used to. To customize the session state, the user simply calls copyWith.
    • The only thing that can be overridden with copyWith at the moment is authenticationInfo. As we go forward we will explore what the needs are and extend with more properties here, e.g. cache and message central.
  • By default, all tests are wrapped in database transactions which automatically rolls back the database after each test case

See the tests/serverpod_test_server/test_integration/test_tools/* folder for comprehensive examples of all capabilities.

💡 The test tools will be generated if the server_test_tools_path key is specified in config/generator.yaml. Since the templates are not updated in this PR, this is not exposed by default. Once it's production ready, the template will specify a default test tools path.

💡 As you will see, withServerpod is not exported by serverpod_test directly but from the generated test tools file. The main benefit from doing it like this is that this completely removes the need for boiler plate code. Since using generated code is already done in other places, it doesn't go against the paradigm of the Serverpod framework.

Changes

  • Adds test helpers, as described above
  • Adds new test helper code generation module
  • Adds new small serverpod_test package that contains the business logic for the test helper
  • Adds code to prepare for running Serverpod in test mode, but will be finished in a follow-up PR (see Follow-up issues below)
  • Adds test for the test helper in tests/serverpod_test_server/test_integration/test_tools/*. The goal is that these tests should be the source of truth of all the capabilities of the test helper. Therefore, these tests are written from the perspective of an end user.
  • Adds unit tests for the test code generation
    • 💡 Note that I limited the tests to the public interfaces. That the internal implementation works is validated by all the integrations tests, as well as the linter which would not build our project if there were type errors.
  • Adds DatabaseAccessor interface to enable the automatic rollback feature in tests

Recommended reviewing

  • Start by looking at the tests/serverpod_test_server/test_integration/test_tools/* folder to see how the test tool is used for different use cases
  • I tried committing in logical order, so reviewing commit by commit should be best
  • Please be explicit about wether you think something is missing in this PR or if it's a suggestion for a follow-up PR.

Follow-up issues [WIP]

There are a few things that needs to be fixed before this can be exposed publicly. See full list below:

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

@hampuslavin
hampuslavin force-pushed the hl_test_framework_with_serverpod branch 4 times, most recently from 8a2b96e to daf648e Compare September 10, 2024 14:49
@hampuslavin hampuslavin changed the title [WIP] feat: Add Serverpod test tools feat: Add Serverpod test tools Sep 10, 2024
@hampuslavin
hampuslavin force-pushed the hl_test_framework_with_serverpod branch 3 times, most recently from 5883bf6 to 35d6da5 Compare September 11, 2024 07:07
@hampuslavin
hampuslavin force-pushed the hl_test_framework_with_serverpod branch from 35d6da5 to 77e4726 Compare September 11, 2024 07:16
@hampuslavin
hampuslavin marked this pull request as ready for review September 11, 2024 07:18

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Dumping a first session of comments now after looking at the tests. Review is not complete but it seems like my review tab is slowing down.

Comment thread util/run_tests_analyze Resolved

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome work with all of this.
I have a bunch of comments and questions.

Please look them through and ping me when it is time for a re-review or if you have any questions.

The changelog in the new project should inlcude all the other changelogs.

I think you can add these by running the util/update_pubspecs script

Comment thread util/run_tests_analyze Resolved
Comment thread packages/serverpod_test/.gitignore Resolved
@hampuslavin
hampuslavin force-pushed the hl_test_framework_with_serverpod branch from 5606bd4 to b257a14 Compare September 16, 2024 11:07
@hampuslavin hampuslavin mentioned this pull request Sep 17, 2024
4 of 6 tasks

@Isakdl Isakdl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is huge! Been on my whish list since day one ❤️!

LGTM 🚀 🚀 🚀

@Isakdl
Isakdl dismissed SandPod’s stale review September 17, 2024 12:26

Comments fixed

@hampuslavin
hampuslavin merged commit 22ee134 into serverpod:main Sep 17, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants