Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the default domain of Client() in unittest of Django

Tags:

I am writing a unit test for Django views.

class TestLog(unittest.TestCase):
    """Test for Contact"""
    def setUp(self):
        self.c = Client()
        try:
            self.bob = User.objects.create_user("mojo","[email protected]", "bmojo")
        except :
            print ''

    def test_get_emails(self):
        response = self.c.get('/text/')
        self.assertEqual(response.status_code, 200)


    def test_htmlemils(self):
        response = self.c.get('/emails/html/upload')
        self.assertEqual(response.status_code, 200)

The c = Client() takes the 'http://testserver' as domain which i want to overwrite ,i want to add my real domain in that test client ,is their way to customize the test Client ?

like image 938
Shashi Avatar asked Jun 09 '11 10:06

Shashi


People also ask

What is RequestFactory in Django?

The request factory The API for the RequestFactory is a slightly restricted subset of the test client API: It only has access to the HTTP methods get() , post() , put() , delete() , head() , options() , and trace() . These methods accept all the same arguments except for follow .

Does Django use Pytest or Unittest?

Writing testsDjango's unit tests use a Python standard library module: unittest . This module defines tests using a class-based approach.

What is self client in Django?

self. client , is the built-in Django test client. This isn't a real browser, and doesn't even make real requests. It just constructs a Django HttpRequest object and passes it through the request/response process - middleware, URL resolver, view, template - and returns whatever Django produces.

What is Unittest in Django?

Unit Tests are isolated tests that test one specific function. Integration Tests, meanwhile, are larger tests that focus on user behavior and testing entire applications. Put another way, integration testing combines different pieces of code functionality to make sure they behave correctly.


1 Answers

Django's Client extends RequestFactory so you should be able to pass in extra params as keyword arguments.

Try:

response = self.c.get('/emails/html/upload', SERVER_NAME="mydomain.com")
like image 132
adamnfish Avatar answered Oct 19 '22 22:10

adamnfish