Reputation: 343
I have written few functions in my base page and I am trying to call it in my test. However, if one of the functions fails, the test does not go further. How I can achieve this using selenium python so the test keeps running and give the errors later.
Upvotes: 1
Views: 1438
Reputation: 343
I have used the following approach to achieve this. Is this the right approach?
class mytest(BaseTestCase, unittest.TestCase):
def setUp(self):
super(mytest, self).setUp()
def test_one(self):
# content
def test_two(self):
# content
def test_three(self):
# content
def test_four(self):
# content
def test_five(self):
# content
def test_six(self):
# content
def tearDown(self):
super(mutest, self).tearDown()
Upvotes: 0
Reputation: 1731
In Python unit testing using either unittest.TestCase
classes or the django.test.TestCase
classes, you should create one test_*
function for each logical concept that you want to test. A unit test should fail for only one reason. It is ok to have multiple ways to test for the one reason but the test should fail for only one reason.
A test class may be as follows:
from django.test import LiveServerTestCase
from selenium import webdriver
class FooTestCase(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
[...]
def tearDown(self):
self.browser.quit()
[...]
def test_thing_one(self):
self.browser.get(self.live_server_url)
self.assertIn('My Title', self.browser.title)
def test_thing_two(self):
[...]
self.assertTrue(foobar)
In this class there are 2 test_*
functions. When you run this test case and both pass you would see output similar to:
..
-------------------------------
Ran 2 tests in 0.213s
It tells us it ran two tests. Notice that it also has two periods. One for each test. Period means test passed. Both passed. If the first test failed we would see:
F.
===============================
FAIL: test_thing_one (mytests.FooTestCase)
-------------------------------
Traceback (most recent call last):
# stack trace here
AssertionError: (rest of error)
-------------------------------
Ran 2 tests in 0.213s
FAILED (failures=1)
This time one of the periods has been replaced with the letter F. There is still one period which represents a test that passed. Notice that the F comes first. This means that test_thing_one
ran first and failed and even though it failed test_thing_two
still ran normally and passed.
In test_thing_one
we could have used multiple assertions as follows:
def test_thing_one(self):
[...]
self.assertIn(x, bar_list)
self.assertTrue(something)
self.assertIsInstance(a, b)
Now if assertTrue
fails test_thing_one will stop at the point and assertIsInstance
will never be called. This is correct behavior. If you still want assertIsInstance
to be called even if assertTrue
fails then you should create a new test_*
method and move assertIsInstance
to that method.
In general you should have only one assert*
per test_*
. This will help you limit it to testing only one concept. If these three asserts are testing only one concept this is ok, as you will know where to go in your code to fix that one small thing. If assertTrue
tests one piece of code and assertIsInstance
tests another piece of code they should be in two separate test_*
methods.
If you have 6 test cases and your are not seeing a combination of 6 periods or F
then something else abnormal is happening. If that is the case please update your question with the errors so we can fix that problem.
Upvotes: 1