在Python中为SeleniumBase运行pytest时如何从其他函数获取输入变量?

问题描述

上传到网站的文件每次运行都会更改,我希望有一种方法可以在测试方法中进行更改。目前,我将其设置如下:@H_404_1@

functions.py @H_404_1@

def get_file():
    ...
    file = file_ex1.doc
    return file

def run_test1():
    cmd = "pytest -v webtests.py::EF --browser=chrome"
    subprocess.run(split(cmd))  # run command line
...

webtests.py @H_404_1@

file_path = file  # changes with each run,should come from get_file()

class EF(BaseCase):
    def test_Now(self):
        self.open('https://www...')
        self.find_element('input[name="Query"]').send_keys(file_path)`

我想知道基于functions.py中某些函数输出来更改file_path变量的最佳方法。例如,有时可能会产生一个名为file1的文件以进行上传,或者有时会产生filet2.txt。连接两个脚本并使用每次运行的更新文件路径运行test_Now()的最佳方法是什么?还是应该以其他方式设置它们?@H_404_1@

非常感谢。@H_404_1@

解决方法

您可以使用SeleniumBase随附的pytest args。

--data=DATA  # (Extra test data. Access with "self.data" in tests.)
--var1=DATA  # (Extra test data. Access with "self.var1" in tests.)
--var2=DATA  # (Extra test data. Access with "self.var2" in tests.)
--var3=DATA  # (Extra test data. Access with "self.var3" in tests.)

来源:Docs