mirror of
https://github.com/django-components/django-components.git
synced 2025-08-04 14:28:18 +00:00
docs: update testing, single file comp, and syntax highlight (#1109)
This commit is contained in:
parent
b6994e9ad3
commit
9ede779fa3
5 changed files with 232 additions and 146 deletions
|
@ -4,7 +4,10 @@ The [`@djc_test`](../../../reference/testing_api#djc_test) decorator is a powerf
|
|||
|
||||
## Usage
|
||||
|
||||
The [`@djc_test`](../../../reference/testing_api#djc_test) decorator can be applied to functions, methods, or classes. When applied to a class, it recursively decorates all methods starting with `test_`, including those in nested classes. This allows for comprehensive testing of component behavior.
|
||||
The [`@djc_test`](../../../reference/testing_api#djc_test) decorator can be applied to functions, methods, or classes.
|
||||
|
||||
When applied to a class, it decorates all methods starting with `test_`, and all nested classes starting with `Test`,
|
||||
recursively.
|
||||
|
||||
### Applying to a Function
|
||||
|
||||
|
@ -15,8 +18,6 @@ simply decorate the function as shown below:
|
|||
import django
|
||||
from django_components.testing import djc_test
|
||||
|
||||
django.setup()
|
||||
|
||||
@djc_test
|
||||
def test_my_component():
|
||||
@register("my_component")
|
||||
|
@ -27,38 +28,34 @@ def test_my_component():
|
|||
|
||||
### Applying to a Class
|
||||
|
||||
When applied to a class, `djc_test` decorates each `test_` method individually:
|
||||
When applied to a class, `djc_test` decorates each `test_` method, as well as all nested classes starting with `Test`.
|
||||
|
||||
```python
|
||||
import django
|
||||
from django_components.testing import djc_test
|
||||
|
||||
django.setup()
|
||||
|
||||
@djc_test
|
||||
class TestMyComponent:
|
||||
def test_something(self):
|
||||
...
|
||||
|
||||
class Nested:
|
||||
class TestNested:
|
||||
def test_something_else(self):
|
||||
...
|
||||
```
|
||||
|
||||
This is equivalent to applying the decorator to each method individually:
|
||||
This is equivalent to applying the decorator to both of the methods individually:
|
||||
|
||||
```python
|
||||
import django
|
||||
from django_components.testing import djc_test
|
||||
|
||||
django.setup()
|
||||
|
||||
class TestMyComponent:
|
||||
@djc_test
|
||||
def test_something(self):
|
||||
...
|
||||
|
||||
class Nested:
|
||||
class TestNested:
|
||||
@djc_test
|
||||
def test_something_else(self):
|
||||
...
|
||||
|
@ -70,19 +67,26 @@ See the API reference for [`@djc_test`](../../../reference/testing_api#djc_test)
|
|||
|
||||
### Setting Up Django
|
||||
|
||||
Before using [`djc_test`](../../../reference/testing_api#djc_test), ensure Django is set up:
|
||||
If you want to define a common Django settings that would be the baseline for all tests,
|
||||
you can call [`django.setup()`](https://docs.djangoproject.com/en/5.2/ref/applications/#django.setup)
|
||||
before the `@djc_test` decorator:
|
||||
|
||||
```python
|
||||
import django
|
||||
from django_components.testing import djc_test
|
||||
|
||||
django.setup()
|
||||
django.setup(...)
|
||||
|
||||
@djc_test
|
||||
def test_my_component():
|
||||
...
|
||||
```
|
||||
|
||||
!!! info
|
||||
|
||||
If you omit [`django.setup()`](https://docs.djangoproject.com/en/5.2/ref/applications/#django.setup)
|
||||
in the example above, `@djc_test` will call it for you, so you don't need to do it manually.
|
||||
|
||||
## Example: Parametrizing Context Behavior
|
||||
|
||||
You can parametrize the [context behavior](../../../reference/settings#django_components.app_settings.ComponentsSettings.context_behavior) using [`djc_test`](../../../reference/testing_api#djc_test):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue