0

I'm working on automated testing in pytest, and i'm looking for a way to read params from a config file that are specific to a test and add it to the appropriate test.

for example I would like my config.ini file to look like this:

    [Driver]
    #some genral variables

    [Test_exmpl1]
    #variables that I would like to use in Test_exmpl1
    username= exmp@gmail.com
    password= 123456

    [Test_exmpl2]
    #variables that I would like to use in Test_exmpl2
    username= exmp2@gmail.com
    password= 123456789

Now in the code I would like to be able to use these params in the correct test:

class Test_exmpl1(AppiumTestCase):

    def test_on_board(self):

        self.view = Base_LoginPageObject()
        # view = BaseLoginPageObject
        self.view = self.view.login(config.username, config.password)
        #config.username =exmp@gmail.com
        #config.password = 123456

class Test_exmpl2(AppiumTestCase):

    def test_on_board(self):

        self.view = Base_LoginPageObject()
        # view = BaseLoginPageObject
        self.view = self.view.login(config.username, config.password)
        #config.username =exmp2@gmail.com
        #config.password = 123456789

Does anyone have an idea how I should go about doing that?

NotSoShabby
  • 3,316
  • 9
  • 32
  • 56

1 Answers1

0

conftest.py

 @pytest.fixture()
    def before(request):
        print("request.cls name is :-- ",request.cls.__name__)
        if request.cls.__name__ == 'Test_exmpl1':
            return["username","password"]
        elif request.cls.__name__ == 'Test_exmpl2':
            return["username2","password2"]
   

test_module.py

import pytest

class Test_exmpl1():

    def test_on_board(self,before):
        print("IN CLASS 1")
        print("username :-- %s and password is %s"%(before[0],before[1]))

class Test_exmpl2():

    def test_on_board(self,before):
        print("IN CLASS 2")
        print("username :-- %s and password is %s"%(before[0],before[1]))

You can create a file conftest.py like as above and you can use those values in your test file of pytest.

Community
  • 1
  • 1
nikhilesh_koshti
  • 393
  • 1
  • 10
  • Thanks for your reply! can you elaborate a little on the before method and the pytest.fixture? I read some py.test documentation but it wasn't vary clear. The "request" param the before method gets is the instance of the test class? is it called before each test with the relevant class? – NotSoShabby Oct 26 '17 at 11:33
  • Any method with the decorator @pytest.fixture will always called before the test functions that contains the method names as a parameter. Request is not any class instance,but it holds the information that we required when executing the fixture method. i.e:--In this case,"before" is fixture method which is called before the both class method. – nikhilesh_koshti Oct 26 '17 at 13:39