2017-04-18 291 views
1

我已經運行硒測試中pytest測試項目有如下的結構:如何在一次測試中使用多個pytest conftest文件並使用重複的parser.addoption?

ProjRoot 
| 
|_Pytest.ini 
|_____________TestFolderA 
|     | 
|     |_test_folderA_tests1.py 
|     |_test_folderA_tests2.py 
| 
|____________TestFolderB     
|     | 
|     |_test_folderB_test1.py 
|     |_test_folderA_tests2.py 
|     
| 
|___________TestHelperModules 
|     | 
|     |_VariousTestHelperModules 
| 
|____________DriversAndTools 
        |___(contains chromedriver.exe, firefox profile folder etc) 

我有一個confTest.py文件,我目前在ProjRoot,這是我作爲一個安裝使用和拆除建立運行運行的每個測試的瀏覽器會話。它運行每個測試兩次。一次用於Chrome,一次用於Firefox。在我的測試中,我只是使用最終的驅動器夾具。該conftest文件如下:

#conftest.py 

import pytest 
import os 
import rootdir_ref 
from selenium.webdriver.common.keys import Keys 
import time 

from webdriverwrapper.pytest import * 
from webdriverwrapper import Chrome 
from webdriverwrapper import DesiredCapabilities 
from webdriverwrapper import Firefox 
from webdriverwrapper import FirefoxProfile 



#when running tests from command line we should be able to pass --url=www..... for a different website, check what order these definitions need to be in 
def pytest_addoption(parser): 
    parser.addoption('--url', default='https://test1.testsite.com.au') 

@pytest.fixture(scope='function') 
def url(request): 
    return request.config.option.url 

browsers = { 
    'firefox': Firefox, 
    'chrome': Chrome, 
} 

@pytest.fixture(scope='function', 
       params=browsers.keys()) 
def browser(request): 

    if request.param == 'firefox': 
     firefox_capabilities = DesiredCapabilities.FIREFOX 
     firefox_capabilities['marionette'] = True 
     firefox_capabilities['handleAlerts'] = True 
     theRootDir = os.path.dirname(rootdir_ref.__file__) 
     ffProfilePath = os.path.join(theRootDir, 'DriversAndTools', 'FirefoxSeleniumProfile') 
     geckoDriverPath = os.path.join(theRootDir, 'DriversAndTools', 'geckodriver.exe') 
     profile = FirefoxProfile(profile_directory=ffProfilePath) 
     print (ffProfilePath) 
     print (geckoDriverPath) 
     b = browsers[request.param](firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoDriverPath) 

    elif request.param == 'chrome': 
     desired_cap = DesiredCapabilities.CHROME 
     desired_cap['chromeOptions'] = {} 
     desired_cap['chromeOptions']['args'] = ['--disable-plugins', '--disable-extensions'] 
     theRootDir = os.path.dirname(rootdir_ref.__file__) 
     chromeDriverPath = os.path.join(theRootDir, 'DriversAndTools', 'chromedriver.exe') 
     b = browsers[request.param](chromeDriverPath) 

    else: 
     b = browsers[request.param]() 
    request.addfinalizer(lambda *args: b.quit()) 

    return b 


@pytest.fixture(scope='function') 
def driver(browser, url): 
    driver = browser 
    driver.maximize_window() 
    driver.get(url) 
    return driver 

我希望做的是在每個測試文件夾,而不是一個ProjRoot文件conftest。但是,如果我藉此現有conftest文件,並把它放在每個test文件夾,然後使用

python –m pytest 

讓pytest皮卡從pytest.ini測試目錄(預計測試文件夾與運行運行從項目的根pytest其分別包含conftest文件)我有問題的parser.addoption --url已經被添加。錯誤消息的結尾是:

ClientScripts\conftest.py:19: in pytest_addoption 
    parser.addoption('--url', default='https://test1.coreplus.com.au/coreplus01') 
..\..\..\VirtEnv\VirtEnv\lib\site-packages\_pytest\config.py:521: in addoption 
    self._anonymous.addoption(*opts, **attrs) 
..\..\..\VirtEnv\VirtEnv\lib\site-packages\_pytest\config.py:746: in addoption 
    raise ValueError("option names %s already added" % conflict) 
E ValueError: option names {'--url'} already added 

的--url addoption的目的是這樣我就可以覆蓋在命令行中conftest文件的默認設置,如果我想在同樣的將它們全部指向一個不同的URL時間,但是否則讓他們默認運行到不同的url,如其conftest文件中指定的那樣。

回答

0

我有類似的問題。 刪除所有緩存的文件和venv後,錯誤消失了。

相關問題