科技公司的网站建设费入什么科目,网站建设技术托管,国内大型电子网站建设,oa办公系统网址背景
接上一篇#xff0c;【大模型问答测试】大模型问答测试脚本实现#xff08;第一版#xff09;。 在实现自动化的时候#xff0c;原先把很多方法与request请求写在一块了#xff0c;趁着目前实现接口数量较少#xff0c;决定对代码进行解耦#xff0c;并且清晰目录…背景
接上一篇【大模型问答测试】大模型问答测试脚本实现第一版。 在实现自动化的时候原先把很多方法与request请求写在一块了趁着目前实现接口数量较少决定对代码进行解耦并且清晰目录结构顺便接入pytest为后续实现excel/yaml文件驱动以及接入allure测试报告做准备。
代码解耦
从git重新拉了一个分支这样就确保不会影响到原来的代码也便于代码的回滚 设计逻辑将原先问答模块的代码拆分拆分为发送request的模块以及相应的功能方法模块便于后期进行维护与扩展
接入pytest
原先发送请求的模块暂调整为testCase里面src核心代码模块先保留后期设计发送各种请求模块例如get与post请求操作mysql模块以及数据驱动模块等等。
根据pytest的使用逻辑进行接入pytestpytest使用规则如下
pytest使用规则
一、测试函数命名规则 测试函数必须以 test_ 开头。 例如def test_addition():。只有以 test_ 开头的函数才会被 pytest 自动识别为测试函数并执行。
二、测试类命名规则 测试类必须以 Test 开头。 例如class TestMyClass:。 测试类中的方法也需要遵循测试函数的命名规则即以 test_ 开头才能被识别为测试方法。
三、断言的使用 使用 assert 语句进行断言。 例如assert result expected用于验证实际结果与预期结果是否一致。 如果断言失败pytest 将报告详细的错误信息包括失败的断言语句和实际值与预期值的差异。
四、命令行参数 运行测试时可以使用各种命令行参数来控制测试的执行。 -v或 --verbose详细模式显示更多的测试执行信息。 -s允许测试中的标准输出如 print 语句的输出显示在终端上。 -k EXPRESSION根据关键字表达式运行特定的测试。例如pytest -k “addition” 将只运行名称中包含 “addition” 的测试。 -x一旦遇到第一个失败的测试就停止运行。 –maxfailN在遇到 N 个失败的测试后停止运行。
五、Fixture 的使用 Fixture 是 pytest 中用于提供测试所需的资源或设置测试环境的机制。 使用 pytest.fixture 装饰器定义 Fixture。 例如 pytest.fixturedef setup_data():return [1, 2, 3]在测试函数中可以将 Fixture 作为参数传入以使用其提供的资源。 例如def test_function(setup_data):在这个测试函数中可以使用 setup_data 参数来访问 Fixture 提供的数据。
六、参数化测试 使用 pytest.mark.parametrize 装饰器可以实现参数化测试。 例如
pythonpytest.mark.parametrize(input1, input2, expected, [(1, 2, 3),(4, 5, 9),(6, 7, 13)])def test_addition(input1, input2, expected):assert input1 input2 expected这个测试函数将被多次执行每次使用不同的参数组合。
七、测试用例组织 可以将相关的测试函数组织在同一个测试类中或者将不同功能的测试放在不同的测试文件中。这有助于提高测试的可读性和可维护性。
八、跳过和标记测试 使用 pytest.mark.skip 装饰器可以跳过某个测试。 例如pytest.mark.skip(reason“测试功能尚未实现”)。 使用 pytest.mark.xfail 装饰器标记一个预期会失败的测试。 当这个测试失败时pytest 将报告它为 “预期失败”而不是普通的失败。
九、插件的使用 pytest 有很多插件可以扩展其功能。 例如pytest-html 插件可以生成 HTML 格式的测试报告。 安装插件后可以在命令行中使用相应的参数来启用插件的功能。
十、配置文件 pytest 可以使用 pytest.ini 配置文件来设置一些全局的配置选项。 例如可以在配置文件中设置测试的搜索路径、命令行参数的默认值等。 根据上面pytest规则的介绍因此我们需要改动目录命名与类与方法的命名 最后进行验证代码正常执行
思考
其实接入pytest也是为后续使用Jenkins做CICD做准备因为在配置cicd的时候需要使用到一个启动的命令行也就是需要进行配置而pytest刚好又支持这种用法因此也算是一拍即合 文章转载自: http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.trrhj.cn.gov.cn.trrhj.cn http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.bmqls.cn.gov.cn.bmqls.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.rnpnn.cn.gov.cn.rnpnn.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.zqmdn.cn.gov.cn.zqmdn.cn http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.pghgq.cn.gov.cn.pghgq.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.rqkk.cn.gov.cn.rqkk.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.jbxd.cn.gov.cn.jbxd.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.ccdyc.cn.gov.cn.ccdyc.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.jmspy.cn.gov.cn.jmspy.cn http://www.morning.redhoma.com.gov.cn.redhoma.com