Unit Tests - OmnipreneurshipAcademy/edx-platform GitHub Wiki
Unit Tests
Following is the link to understand the Circleci and unit testing flow for ADG, It also includes guidelines on how to perform tests locally for lms/cms. Here is the link to the session by @zahid-ali. Thanks to @zahid-ali for the session and detailed slides.
Guidelines:
Recommended:
- Use
is_testing_environment
method from utils whenever you are doing core changes. If env in a test env then let the core code execute else our changed code will be executed. You can find examples in the code.
- Write unit tests for all the changes.
- Always fix core unit tests after making core code changes (May be by patching or Waffle Switches).
- Write unit tests for new features and also for the core changes that you made but all of these will go to the adg/ directory.
suspendingreceiver
Use from openedx.adg.lms.utils.decorators import suspendingreceiver
instead of from django.dispatch import receiver
, if you want your receiver not to catch any signal. We can mute ADG signals but we cannot mute Edx signal, so this is the way to avoid executing the receiver code.
In-order to write unit tests of such a receiver see this pull request
mute_signal
If you want to mute signal generating from your unit test, you can use mute_signal
fixture, via annotation @pytest.mark.mute_signals
or as unit test param.
Issues and Fixes:
Overridden a core class and updated the imports in core code
If you have overridden a core class and then just imported your class where-ever the core class was being used then the edx core test cases will fail because of the new import. What we can do to avoid this is to patch the new class with the older one in the tests so that the test cases will pass.
A sample for that is:
patcher = mock.patch('openedx.core.djangoapps.user_authn.views.register.RegistrationFormFactory', new=RegistrationFormFactory)
self.mock_class = patcher.start()
self.addCleanup(patcher.stop)
- Add the above code in setUp of your tests.
- You will have to update the classpath and the new argument.
- Path MUST be the path of usage but not where it is defined. Like in above example I used RegistrationFormFactory in the file register.py so I want add patch for register.py
- The new argument contains the old class which I want to use instead of my new class which is the core class which I have overridden.