Hello , I want to invoke constructor of same class while running pytest | Selenium Python Forum
D
Deepak Kumar Tiwari Posted on 11/12/2020
from app_pages.base import BaseClass
from app_pages.landing_page import LandingPage
from common_util.util import framework_util
import pytest 


class test_landing_page(BaseClass): 
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
    # super().__init__()
        driver = self.launch_browser()
        lp = LandingPage(driver)
        u = framework_util(driver)

        
    # global driver
def test_landing_page_functionality_one():
        print("Inside functionality one test cases")
        # print(driver.session_id)
    
def test_landing_page_functionality_two():
        print("Inside functionality two test cases")  
        # print(driver.session_id)
        
def test_landing_page_functionality_three():
        print("Inside functionality three test cases")  
        # print(driver.session_id) 
        
####--------------Not invoking __init__(self)

from app_pages.base import BaseClass
from app_pages.landing_page import LandingPage
from common_util.util import framework_util

class test_landing_page(BaseClass):    
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        driver = self.launch_browser()
        lp = LandingPage(driver)
        u = framework_util(driver)
        
    #global driver
    def test_landing_page_functionality_one(self):
        print("Inside functionality one test cases")
        print(self.driver.session_id)
    
    def test_landing_page_functionality_two(self):
        print("Inside functionality two test cases")  
        print(self.driver.session_id)
        
    def test_landing_page_functionality_three(self):
        print("Inside functionality three test cases")  
        print(self.driver.session_id)   
        self.logout_from_application()
        self.close_browser()
   
def main():
    t = test_landing_page()
    t.test_landing_page_functionality_one()
    t.test_landing_page_functionality_two()
    t.test_landing_page_functionality_three()
     
if __name__ == '__main__':main()  

##-------------------This one is invoking constructor ----------------##

##--Please help ---      

D
Deepak Kumar Tiwari Replied on 11/12/2020

Hi , I think the recommended way is to put pytest function with in class.

def test_landing_page_functionality_one():
    print("Inside functionality one test cases")
 # print(driver.session_id)

def test_landing_page_functionality_two():
     print("Inside functionality two test cases")
     # print(driver.session_id)

def test_landing_page_functionality_three():
     print("Inside functionality three test
     # print(driver.session_id)

 

I am assuming that pytest functions are needed to be kept outside class declaration.


D
Deepak Kumar Tiwari Replied on 12/12/2020

#-----In eclipse ----------#

from app_pages.base import BaseClass
from app_pages.landing_page import LandingPage
from common_util.util import framework_util
import pytest 

class Test_landing_page(BaseClass): 
    '''
    classdocs
    '''
    global driver
    def __init__(self):
        '''
        Constructor
        '''
        driver = self.launch_browser()
        lp = LandingPage(driver)
        u = framework_util(driver)

    @pytest.mark.ui(order=1)
    def test_setup(self):
        print("Inside setup function")  
             
    @pytest.mark.ui(order=2)
    def test_landing_page_functionality_one(self):
        print("Inside functionality one test cases")
        # print(driver.session_id)
    
    @pytest.mark.ui(order=3)
    def test_landing_page_functionality_two(self):
        print("Inside functionality two test cases")  
        # print(driver.session_id)
    
    @pytest.mark.ui(order=4) 
    def test_landing_page_functionality_three(self):
        print("Inside functionality three test cases")  
        # print(driver.session_id) 
        
    @pytest.mark.ui(order=5)
    def test_teardown(self):
        print(driver.session_id)


============================= test session starts ==============================
platform linux -- Python 3.7.5, pytest-3.10.1, py-1.8.0, pluggy-0.12.0 -- /usr/bin/python3.7
cachedir: .pytest_cache
rootdir: /home/deepak/softwares/eclipsewspace/GUIAutomationP1, inifile: pytest.ini
collecting ... collected 0 items

=============================== warnings summary ===============================
test_cases/test_landing_page3.py:11
  /home/deepak/softwares/eclipsewspace/GUIAutomationP1/test_cases/test_landing_page3.py:11: PytestWarning: cannot collect test class 'Test_landing_page' because it has a __init__ constructor
    class Test_landing_page(BaseClass):

-- Docs: https://docs.pytest.org/en/latest/warnings.html
========================== 1 warnings in 0.15 seconds ==========================

https://github.com/pytest-dev/pytest/issues/410


D
Deepak Kumar Tiwari Replied on 14/12/2020

# ------ Below is work around --------


from app_pages.base import BaseClass
from app_pages.landing_page import LandingPage
import pytest 

class Test_landing_page(BaseClass): 
    '''
    classdocs
    '''
    @pytest.mark.ui(order=1)
    def test_setup(self):
        print("Inside functionality one setup test cases")
        setup(self)
        lp.sign_in_to_app('xxxx', 'yyyy')
        
    @pytest.mark.ui(order=2)
    def test_landing_page_functionality_one(self):
        print("Inside functionality one test cases")
       # self.lp.page_title_information()
        print("=============")
        print(driver.session_id)
    
    @pytest.mark.ui(order=3)
    def test_landing_page_functionality_two(self):
        print("Inside functionality two test cases")  
        print("=============")
        lp.event_one()
      #print(self.driver.session_id)
    
    @pytest.mark.ui(order=4) 
    def test_landing_page_functionality_three(self):
        print("Inside functionality three test cases")  
        #print(self.driver.session_id) 
        
    @pytest.mark.ui(order=5)
    def test_teardown(self):
        print("Inside functionality last test cases")   
        #print(self.driver.session_id) 
        tear_down(self)
        
def setup(self):
    #global driver
    self.driver = self.launch_browser()
    self.lp = LandingPage(self.driver)
    global driver,lp
    driver = self.driver
    lp = self.lp
           
def tear_down(self):
    print("Inside tear down test case") 
    lp.kill_session()
     


0
09914040666 Replied on 24/12/2020

Hey, 

Thank you if you are providing a different work around for the issue. If there is a question inside the whole query, then I didnt understand it properly. 

Please do let me the query if you have any.


Related Posts