apluslms / a-plus

A+ frontend portal - A+ LMS documentation:
https://apluslms.github.io/
Other
64 stars 72 forks source link

Use setUpTestData in tests #1348

Closed sayravai closed 3 months ago

sayravai commented 4 months ago

Fixes #1233

Use setUpTestData instead of setUp for creating test data for a-plus unit tests.

What?

Unit tests in a-plus have used the setUp method for creating database entries for tests, which causes them to be created and destroyed separately for each test inside a test class. There is an alternative method called setUpTestData, which only creates the entries once and reuses the data for the whole test class, resulting in a considerable speedup when running tests.

Why?

Current a-plus unit tests require roughly 1/3 of the time required before this change. On my laptop: Using setUp: Ran 245 tests in 232.683s Using setUpTestData: Ran 245 tests in 70.508s

How?

Basically the only thing changed is replacing the setUp instance methods with setUpTestData class methods. In a few cases, the api tests used the same dataset as "normal" tests (course, exercise), so here a custom testcase class (with data definitions) was defined and inherited to both classes instead of TestCase. The edit_course > operations was not converted due to issues, but those tests take like a second anyway, so probably not worth a deeper investigation at this point.

Fixes #1233

Testing

The unit test suite was successfully ran using the updated code, but of course I cannot be sure these changes do not cause any tests to pass when they should not.

What type of test did you run?

Did you test the changes in

Translation

Programming style

Have you updated the README or other relevant documentation?

Is it Done?

Clean up your git commit history before submitting the pull request!