1

I am writing a test case using selenium web driver for python. Test case has multiple methods like :

class Test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver1 = webdriver.Firefox()
        cls.driver1.maximize_window()

    def test_1(self):
        .....
        .....
    def test_2(self):
        ....
        ....
    @classmethod
    def tearDownClass(cls):
        cls.driver1.quit()

When i try to run the file from pycharm or terminal it executes the methods randomly. I want a sequential execution, so test_1() should run before test_2(). Please help me.. Thanks in advance.

2 Answers2

1

Syntax for running multiple tests in single instance of browser is correct; though it does not runs tests in random order,rather it does runs the test in numerical or alphabetical order.

ex: class Test(unittest.TestCase):

@classmethod
def setUpClass(cls):
    .....
def test_1(self): #or def test_a
    print 'test1'

def test_3(self): #or def test_c
    print 'test2'

def test_2(self): #or def test_b
    print 'test3'
@classmethod
def tearDownClass(cls):
    .......

test execution order : ascending order i.e test_1 (test_a) will be executed first > next is test_2(test_b) and later test_3(test_c)....irrespective of 'def test_ (self)'( - alphabet or number ) is placed in the code.

o/p: test_1 ok \n test_2.....test3 ok \n test_3.....test2 ok

Don_Manj
  • 131
  • 1
  • 7
-1

Use cucumber along with Selenium, it has feature files where step definitions can be given and all your steps are like mini-tests which are executed in the order they are written. It also has a very layman-readable format of writing steps, Gherkin format.

Should work like a charm.

Here's an example

Feature: User Placing an order via different methods

    Scenario: User tries to place an order for test item
        Given User is on Home Page
        When User Searches for test item
        Then Open the search item page
        And User adds the item to cart
        And User proceeds to book

Here's the link

OR

Give priority to your test methods, this priority is given just above the method definition, at least this is the case in Java, when you use Selenium with TestNG. Not sure how its done in Python. If we give priority to these definitions then they will surely be executed keeping in mind the priority of the methods.

  • dear Rohit Gadia yes i have maintained the sequence like you said but instead the method that runs first is for checking the log out functionality and hence the test fails. Yes i saw the priority feature for java but i was unable to find similar one for python. Thanks any way :) – Sarang Patel Mar 09 '16 at 09:50
  • Cucumber is completely off-topic, and you do not answer the question in any meaningful way. OP wants Python, not Java. – Andrew Regan Mar 09 '16 at 23:16
  • Cucumber step definitions would have helped the process and I was just suggesting and it was a valid suggestion. – Rohit Gadia Mar 10 '16 at 05:20
  • Asking the OP to change the way they work (e.g. BDD), adopt a new tool or language, is not helpful when there are likely much simpler and less disruptive alternatives. – Andrew Regan Mar 10 '16 at 13:14