Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StaticLiveServerTestCase with different domains?

I am using selenium for functional tests with geckodriver and firefox.

I see the host is http://localhost:62305 and this is generated in that class with:

@classproperty
def live_server_url(cls):
    return 'http://%s:%s' % (cls.host, cls.server_thread.port)

In the functionality I am creating, I give the user a way to create a tenant with its own subdomain but for the purpose of the unit test it can be a domain.

So for example I create a tenant with domain example.com, how can I get StaticLiveServerTestCase to point that domain name to the currently running app on that same port, within the same functional test method.

I looked at this post suggesting editing /etc/hosts. The problem with that is it won't just work on CI and other people's computers.

like image 460
tread Avatar asked Sep 01 '18 15:09

tread


People also ask

How do I skip a Django test?

Just trick it to always skip with the argument True : @skipIf(True, "I don't want to run this test yet") def test_something(): ... If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.

What is SimpleTestCase?

SimpleTestCase is the simplest one. It provides the basic features of unittest. TestCase plus some Django extras. It blocks database access by default, because it doesn't do anything to isolate changes you would make there. You should use it for testing components that don't need the database.

What is self client in Python?

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.


1 Answers

You can do this.

class SeleniumTests(StaticLiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        cls.host = 'host.com'
        cls.port = 3000
        super(SeleniumTests, cls).setUpClass()
       ....

in your test class and once you do this for instance

self.selenium.get('%s%s' % (self.live_server_url), 'some_url')

your browser gonna be launch with the right URL like this http://host.com:3000/some_url/, notice how the super call is made after setup the port and host.

like image 99
Reidel Avatar answered Oct 05 '22 22:10

Reidel