Selenium and Django: how to mock the server? - django

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/

Related

How to Selenium test Django project that retrieves data through APIs?

I have a Django Project. I want to make end-to-end tests for it using Selenium.
It interacts with Other components (usually run through docker-compose) API and functions properly.
The Django Project and 'Other Components' are different repositories (obviously there are multiple 'other components')
Testing of parts that don't involve API calls, have been tested.
You can use a continuous integration like Jenkins to do your automated task like Selenium.
It's really easy to install localy and/or to serve.
How to install Jenkins : here
have a great day!

Unexpected behavior with Django LiveServerTestCase using SQLite3

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.

Is it possible to run Django tests on Apache instead of the development server?

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.

Testing phantomJS scripts

I have a phantomjs script that navigates to some pages and store some information about them in a file. Now I want to unit test this script. My problem is that usually the frameworks test my code on client side, so I can't use the PhantomJS API.
I tried to use jasmine-node (https://github.com/mhevery/jasmine-node). It works "server-side", but I can't use all the PhantomJS API because it obviously run my tests with nodejs instead of phantom. Is there a better option to test PhantomJS apps at server-side?
I found two solutions. I think the most complete way to test phantomJS at "server side" is to use CasperJS.
Another solution I have found looking at the phantomjs source code. They use jasmine for testing and it's possible to apply the same idea to my own tests. The run-tests.js is the starting point for this approach.

Django test client response context None

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