As the founder and writer of Euphor.ca, my journey into wellness, psychedelics, and personal growth has been transformative. I created this lifestyle magazine to share valuable insights, knowledge…
To check what ever you write in your application development is it true Or it functioning as per your expectation. when your project is large and large then how you handle correctness of your functionality etc.. 🤗
we just focus on Unit Testing And next Post on focus on functional testing
pip3 install coverage
Instead of a single tests.py
in the app folder you can create a tests
folder where each file holds tests for a single face of the application
Here you can see a tests
folder with api.py
and web.py
. api.py
holds tests for API endpoints, while web.py
can test regular HTML pages. You can also have a models.py
for testing models.
Consider two models: Post and Tag. A post can have many Tag, and an Tag can have many Posts connected. To express this relationship we can apply a ManyToManyField
from Post to Tag.
blog/model.py
Apply the migration:
Lets write test cases for above models: blog/tests/models.py
Now we just created one post with two Tags
Resources:
Some change in ‘blog/model.py’ file
Then Test using ,
create a new file in blog/tests/web.py
. In this file we can import TestCase
, our model, and write a skeleton for the test:
Now to test this feature we need to create some data to send alongside with the POST request. Note that the data should match the model’s fields.
Create new model for blogger this will be the owner of post.
Pest into the ‘blog/tests/web.py’
Now with Django test client we send the request. As a first test we can check if a Blogger instance is created in the database:
Run again :
Show some pages only those who authenticated
Note that you should swap from django.contrib.auth.models import User
with any custom Django user, if present.
Resources:
This was intro about some of the test cases, go first smaller and small test cases and then go though test cases given in book Test-Driven Development with Python written by Harry J.W. Percival
If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you!
The Proxy object enables you to create a proxy for another object, which can intercept and redefine fundamental operations for the object. get method can redefine the access of an atrribute, it…
Evictions in Texas are governed by Chapter 24 of the Texas Property Code. There are three steps to evicting a tenant that is in breach of the lease or holding over beyond the term of the lease…