PythonSelenium:从csv读取数据时如何并行运行测试?

2024-09-29 01:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个selenium测试要运行,这些测试包括读取csv文件、打开浏览器和执行测试。为了减少执行时间,我想并行运行这些测试,我使用了pytest和pytest xdist,在尝试解决这个问题时,我注意到测试使用pytest -s -v tests/test_storage/ -n 2命令打开多个浏览器,如果我不使用csv文件,但当我尝试运行相同的命令但要求从csv读取数据时,它会失败,并出现错误_csv.Error: line contains NUL 这与多进程还是其他什么有关?我不知道怎么解决这个问题

以下是我的page类的外观:

read_data.excel_to_csv(prepFiles.Storagefile,prepFiles.location,fileName=prepFiles.StoragefileName,
                       sheetName='Locations')
csvreader = read_data.getCSVData(prepFiles.location + prepFiles.lifeStoragefileName) # read latest csv

fulllist = []
def findStoreHref(self):
    self.driver.get(self.url)
    stateList = self.getElementList(locator=self.statesFooterXpath,locatorType='xpath') #list of States
    for state in range(len(stateList)):
        self.elementClick(element=stateList[state]) 
        stores = self.waitForAllElementsVisible(locator=self.storelistXpath,locatorType='xpath')
        for storeHref in stores: #loop through stores to get store url
            if self.getHref(element = storeHref,attriType='href') not in self.fulllist:
                self.fulllist.append(self.getHref(element=storeHref, attriType='href'))
        stateList = self.getElementList(locator=self.statesFooterXpath, locatorType='xpath')

我的测试课就是这样的:

@pytest.mark.run(order=1)
def test_Storage_site_csv(self):
    self.ls.findStoreHref()
    first_column =[l[9] for l in self.ls.csvreader]
    list_site_to_csv = [item for item in self.ls.fulllist if item not in first_column]
    self.log.debug(list_site_to_csv)
    assert not list_site_to_csv, 'Test failed. List is not empty. {}'.format(list_site_to_csv)

在一个测试文件夹中有几个页面和测试类,我正在尝试将它们作为一个套件运行。当我使用pytest -s -v tests/test_storage顺序运行套件时,测试运行良好。只有当我尝试以并行方式运行测试时,才会出现错误


Tags: csvtointestselfforreadpytest