Skip to content

Commit

Permalink
Add Mixin and Runner for use with django.test.TestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
tubaman committed May 7, 2022
1 parent 4cf56d2 commit 35840c5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [Defining a custom SMS backend](#defining-a-custom-sms-backend)
- [Signals](#signals)
- [sms.signals.post_send](#sms.signals.post_send)
- [Testing](#testing)
- [Acknowledgement](#acknowledgement)

## Sending SMS
Expand Down Expand Up @@ -228,5 +229,32 @@ Sent after **send()** is called on a **Message** instance. Arguments sent with t
- **sender**: The **Message** class.
- **instance**: The actual **Message** instance being send.

### Testing

**django-sms** provides a couple of classes to make testing sms easier. First,
configure **TEST_RUNNER** in your settings:

```python
TEST_RUNNER = "sms.test.runner.SMSTestRunner"
```

Then use the **SMSTestCaseMixin** in your test cases:

```python
import sms
from django.test import TestCase
from sms.test import SMSTestCaseMixin

class MyTestCase(SMSTestCaseMixin, TestCase):

def test_something_that_sends_sms(self):
do_something_that_sends_sms()
self.assertEqual(len(sms.outbox), 1)
msg = sms.outbox[0]
self.assertEqual(msg.originator, '+12065550100')
self.assertEqual(msg.recipients, ['+441134960000'])
self.assertEqual(msg.body, 'Here is the message')
```

## Acknowledgement
This project is heavily based upon the **django.core.mail** module, with the modified work by [Roald Nefs](https://github.com/roaldnefs). The [Django license](https://raw.githubusercontent.com/roaldnefs/django-sms/main/LICENSE.django) is included with **django-sms**.
10 changes: 10 additions & 0 deletions sms/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sms


class SMSTestCaseMixin:
"""A TestCase mixin that sets up the fake sms backend like Django sets
up the fake email backend
"""
def _pre_setup(self):
super()._pre_setup()
sms.outbox = []
13 changes: 13 additions & 0 deletions sms/test/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sms
from django.conf import settings
from django.test.runner import DiscoverRunner


class SMSTestRunner(DiscoverRunner):
"""A test runner that sets up the fake sms backend like Django sets
up the fake email backend
"""
def setup_test_environment(self, **kwargs):
settings.SMS_BACKEND = "sms.backends.locmem.SmsBackend"
sms.outbox = []
super().setup_test_environment(**kwargs)

0 comments on commit 35840c5

Please sign in to comment.