-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from dimaqq/docs/testing
docs: document how flags can be toggled in tests
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Testing | ||
|
||
## Testing code that depends on flags | ||
|
||
Because `FLAGS` are definable in Django settings, you can use Django's standard `override_settings` to test with a flag off and on. | ||
|
||
```python | ||
from django.test import TestCase, override_settings | ||
|
||
class FlaggedCodeTestCase(TestCase): | ||
@override_settings(FLAGS={"MY_FLAG": [("boolean", True)]}) | ||
def test_flag_enabled(self): | ||
# Do the thing that requires the flag to be enabled | ||
|
||
@override_settings(FLAGS={"MY_FLAG": [("boolean", False)]}) | ||
def test_flag_disabled(self): | ||
# Do the thing that requires the flag to be disabled | ||
``` | ||
|
||
Alternatively, you can create a `FlagState` object along the same lines with a boolean condition that is `True` to test the flag-enabled code path, and then create one with a boolean condition that is` False` to test the not-enabled path. | ||
|
||
|
||
```python | ||
class FlaggedCodeTestCase(TestCase): | ||
def test_flag_enabled(self): | ||
FlagState.objects.create( | ||
name="MY_FLAG", condition="boolean", value="True" | ||
) | ||
# Do the thing that requires the flag to be enabled | ||
|
||
def test_flag_disabled(self): | ||
FlagState.objects.create( | ||
name="MY_FLAG", condition="boolean", value="True" | ||
) | ||
# Do the thing that requires the flag to be disabled | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters