Selenium is great. Not only because it tests on browsers, but also to automate the testing procedure. Especially if you're dealing with forms, where you have to fill the same form several times, to test different combinations, scenarios, etc. To wake up to Selenium was a huge leap forward and I only regret not trying it earlier.
So what about Selenium and Python? What about it? Well, I admit I'm very bias to think that these test libraries and tools only exist in full development for Java and other languages but not Python. Very wrong of me: http://selenium-python.readthedocs.org/
Ok, so I can write python tests with Selenium. I could write the Selenium tests by my self, but being a lazy person, I installed the Selenium extension for Firefox: http://docs.seleniumhq.org/projects/ide/. Just press the REC button, start clicking on your website, and this extension simply records every step you take. Even choices in selects, button clicks, etc. Then the test can be saved and/or exported to python. This means that a completely non-programmer person can find a problem and build a Selenium test, save it and send it via email to a programmer, to explain the problem in a perfect way.
Selenium extension will export a python file that looks like this:
(...) class 1(unittest.TestCase): (...) def test_1(self): driver = self.driver driver.get(self.base_url + "/crm/contact/list/clients/") driver.find_element_by_link_text("Companies").click() driver.find_element_by_link_text("Add Client Company").click() driver.find_element_by_id("id_name").clear() driver.find_element_by_id("id_name").send_keys("aaaaaa") driver.find_element_by_xpath("//button[@type='submit']").click() driver.find_element_by_link_text("Add Company Office").click() driver.find_element_by_id("id_phone").clear() driver.find_element_by_id("id_phone").send_keys("12345678") driver.find_element_by_xpath("//button[@type='submit']").click() Select(driver.find_element_by_id("id_country")).select_by_visible_text("Bahamas") driver.find_element_by_xpath("//button[@type='submit']").click() driver.find_element_by_link_text("aaaaaa - , Bahamas").click() driver.find_element_by_xpath("//button[@type='submit']").click()
And how about Selenium and Django? Some issues arise here. For example, Django has it's own test facility, where a new database is created only for tests, and then it's destroyed. Also, this database can be configured to run on memory, which is much faster than having it be created, populated and destroyed in, say, mysql. But when we start theses tests, they don't fire up a web server, which means, Selenium can't interact with a browser. So, for Selenium tests, we have to use LiveServerTestCase class (only available from Django 1.4 onwards) to create our test classes - this will do the same as the "runserver" does.
The file exported by Selenium Firefox extension has more details, but I only copy the code from the "test_1" function and insert that on my own test function (note the LiveServerTestCase):
class MyClassTest(TestMixin, LiveServerTestCase): (...) def test_1(self): driver = self.browser driver.get(self.base_url + "/crm/contact/list/clients/") (...)
Before running the test, some settings need to be different from my usual development settings, like the database. To do that, I create a new settings file and name it test_settings.py. To run the test:
./manage.py test --settings=settings.test_settings app_to_be_tested
Of course, the file with the tests is named "tests.py" and is inside the "app_to_be_tested" folder. This will run all the tests. To run just some of them:
./manage.py test --settings=settings.test_settings app_to_be_tested.MyClassTest
Or even just one test from one class:
./manage.py test --settings=settings.test_settings app_to_be_tested.MyClassTest.test_1
This is the "how". The "why?" is, of course, all the advantages that methodically testing our work is more safe and saves precious time in the future, when you make some small modification and you can re-run the old tests being sure that all works at least, as good as it worked before. This brings a big assurance, confidence and happy clients. Above all, a better working day and no more crazy Fridays.
The difficult part is it takes more time to get to the finish line and clients get spooked. Not only by the time it takes but also because it costs more money at the beginning. They don't really care if maybe something wrong can happen in the future. Optimism wins over realism when it's cheaper. Also, if we say that the project will have bugs and it's better to do tests from the beginning, the client get's spooked again! "What? This project will have bugs? All projects have bugs? My TV has 30 years and no bugs! You make bad software!".
But this is the every day life of a software company. This is why experienced clients are better - these will ask you for tests.