How to properly unit test XBlock views

I’ve been working on a new XBlock and I have it working when installed into LMS / CMS. However, I’m getting hung up on how to create good unit tests for the XBlock views (e.g. student_view).

The only example I’ve been able to find that actually unit-tests views is edx-ora2, but this is somehow able to import workbench and scenario and other helpers from the xblock-sdk that I am unable to get set up for this XBlock. (Consistently running into the no module found "Workbench" error when I try to do any reference here in my tests, even after installing xblock-sdk dependency.

Any tips / resources would be amazing, I’m definitely getting stuck here.

What you are describing sounds more like integration tests, because they are running using the “workbench” runtime, which is a full XBlock runtime.

There is SeleniumXBlockTest which is now deprecated and unmaintained, but some XBlocks still use it to run integration tests in the workbench, e.g. xblock-poll: xblock-poll/tests/integration at master · open-craft/xblock-poll · GitHub

Closer to unit tests are what Drag and Drop does: xblock-drag-and-drop-v2/tests/utils.py at master · openedx/xblock-drag-and-drop-v2 · GitHub - it still uses the workbench runtime but pretty minimally, I think; most stuff is mocked out so it focuses on the code from the XBlock itself.

Both of these repos use GitHub Actions CI so you should definitely be able to use one of those approaches for any new XBlocks.

In the future, I think a much better approach is needed, either by being able to test XBlocks simply by installing XBlock + Learning Core, or by migrating to a successor to XBlocks that has a much more well-defined API and test infrastructure.