I am using LiveServerTestCase for functional tests with selenium in a Django app. I am using sqlite3 as a backend for local development, and I'm running the tests with a local development server on port 8081.
I wrote a test for user registration, and when I run this test more than once, I see that the test fails because my form throws an error saying the user already exists. When I delete the sqlite3 database file and run re-run migrations I can run the test again and it works fine (but running again will fail).
I thought that this class would use a new database each time a test within the class was run, but I am getting errors.
My question is:
Is there another way to run the tests with LiveServerTestCase so that I don't have to delete the sqlite3 database each time I want to run the test?
Edit:
Here are some other parts of the documentation that my question is based on:
LiveServerTestCase
LiveServerTestCase does basically the same as TransactionTestCase with
one extra feature: it launches a live Django server in the background
on setup, and shuts it down on teardown. This allows the use of
automated test clients other than the Django dummy client such as, for
example, the Selenium client, to execute a series of functional tests
inside a browser and simulate a real user’s actions.
TransactionTestCase
Resetting the database to a known state at the beginning of each test
to ease testing and using the ORM.
Related
My django application uses sparql with jena-fuseki RDF-database. How should I run my unit tests as thete's hardly any information in availble for creatig in-memory jena database for a django unittesting ? So far I've been just running a local fuseki-server, but that's not the way unit testing should be done, right ?
I've been using Django for under a year and would like clarification on the role of tests within a reusable django app. Popular apps, for example, like django-allauth - https://github.com/pennersr/django-allauth - come with tests. From the point of view of the app consumer what is the purpose of these tests? I can see when I run -
python manage.py test
That only MY tests are executed and not the app tests.
Furthermore when I did run the tests -
python manage.py test allauth.account
Pretty much everything failed. For example the first test failed because allauth tried to create a user object with a keyword 'username' which isn't how my project user model is configured (I just have an email field). So these tests which all-auth provides are supposed to run within which project exactly? I'd have thought app designers should create tests which accommodate any proper app consumer project config. That way running the tests informs you whether or not your project is correctly integrated from the consumed apps point of view.
I noticed a similar question but it doesn't really help with my more general question -
How to test single application (not project) in Django?
By the way I've checked my project config for consuming the all-auth app and it does have the right config. As per - https://django-allauth.readthedocs.io/en/latest/advanced.html#custom-user-models, for a user model with a unique email and no username i have -
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
**
On reflection I guess it is not possible for the app designer to write such tests. For example the User model must have a REQUIRED_FIELDS property. The app designer can't know how to populate the project user model based on this property.
So am i right in thinking app tests are only for the app designer? I ask because I'm trying to write my first reusable app.
It's a continuous integration logic: the app test are included in the package to validate that the package is running as expected. As the end user you don't have to run those, because it's already done, usually by Travis, when the package is built/pushed to pypi.
You usually have a nice coverage icon with the % of code covered by the test in the readme of the repo. You'll also have a build on python 3.x and Django 2.x 3.x when available. That's usually done using tox which allow to run the tests on multiple setup while a ./manage.py test will only validate on the current setup.
I won't be too concerned by the fact that the tests aren't running on your specific use case, because the app test are here to validate the package on a "clean" Django install, so the fact that the test isn't running with a email User is normal. There's no reason to make it work on such a user at the app level, unless defining user by email without a login is a feature of the package.
What you are talking about when you say "I'd have thought app designers should create tests which accommodate any proper app" is basically impossible, because something like the User can be defined in 1000 ways. Furthermore it won't be unit tests anymore. This will be integration test, which isn't the responsibility of the app devs. They don't integrate the application, that's the consumer who's doing it and should do its own integration test depending on its use case.
If you want to read more about this I would suggest googling unit tests vs integration tests vs end to end tests.
When I test my Django web application with python manage.py test, this starts Django's development server and runs the test.
I want to run my Django tests on my production stack. Specifically, I want to use Apache instead of Django's development server. Is there a way to do this?
I've looked through the docs for LiveServerTestCase, but it doesn't seem to be customizable in this regard.
The LiveServerTestCase launches a Django server. You can then test it using a client like selenium.
In your case, you are already running the production server that you want to test, that means you don't require the Django server of LiveServerTestCase, or the fixture loading of TestCase. Just use SimpleTestCase. You can then test the live server using a client like selenium.
The Django docs demonstrate how to use selenium. In your case, you just need to set self.live_server_url to the domain that Apache is running on.
I'm starting to introduce Selenium tests to my website that is written in Django. The browser that is controlled by Selenium needs some server to connect. So far I just run my full application in a separate process, but this is painful.
I'd like to run some mock HTTP server, make it serve all the necessary static files and render Django templates and return mock responses to some specific requests.
How would you do that?
Can you not run Selenium over django dev server http://localhost:8000/ .
If not perhaps worth looking at http://harry.pythonanywhere.com/ .where there are some good resources
The best thing is to integrate selenium tests into your unit test suite. When Django 1.4 comes out this will be a supported feature, where the Django test runner will run a development HTTP server for you while the tests run, and load all of your test fixtures for you:
support-for-in-browser-testing-frameworks
LiveServerTestCase
Likely you can't wait until the 1.4 release. In the meantime, you can use something called django-nose-selenium to do this:
https://github.com/weluse/django-nose-selenium
There's a good comprehensive guide on how to do this here:
http://timescapers.com/2011/08/27/django-nose-selenium-a-concise-tutorial/
If I may plug-plug my own tutorial, which will allow you to do full selenium testing against the django test server
http://www.tdd-django-tutorial.com/
I have moved my Django app from my development machine (OS X, Python 2.6.5, Django 1.2.3) to a staging server (Ubuntu VM, Python 2.6.6, Django 1.2.3).
If I now run my test suite on the staging server, two tests fail when using the Django TestClient because response.context is None (but response.content is correct).
For example:
self.assertEquals(self.session.pk, response.context['db_session'].pk)
These test cases pass on the development machine.
Has anybody encountered similar problems?
You need to add the test setup statement.
import django
django.test.utils.setup_test_environment()
Find more details by following my link:
http://jazstudios.blogspot.com/2011/01/django-testing-views.html
From Django documentation:
Although * your code * [+] would work in the Python interactive interpreter, some of the test client's functionality, notably the template-related functionality, is only available while tests are running. The reason for this is that Django's test runner performs a bit of black magic in order to determine which template was loaded by a given view. This black magic (essentially a patching of Django's template system in memory) only happens during test running.
So if you run it in a test run, it should work.
You can see this question