文章轉(zhuǎn)載自:[https://www.cnblogs.com/xiaohuhu/p/9804527.html]
一、用例編寫規(guī)則
1.unittest提供了test cases、test suites、test fixtures、test runner相關(guān)的類,讓測試更加明確、方便、可控。使用unittest編寫用例,必須遵守以下規(guī)則:
(1)測試文件必須先import unittest
(2)測試類必須繼承unittest.TestCase
(3)測試方法必須以“test_”開頭
(4)測試類必須要有unittest.main()方法
2.pytest是python的第三方測試框架,是基于unittest的擴展框架,比unittest更簡潔,更高效。使用pytest編寫用例,必須遵守以下規(guī)則:
(1)測試文件名必須以“test_”開頭或者"_test"結(jié)尾(如:test_ab.py)
(2)測試方法必須以“test_”開頭。
(3)測試類命名以"Test"開頭。
總結(jié): pytest可以執(zhí)行unittest風(fēng)格的測試用例,無須修改unittest用例的任何代碼,有較好的兼容性。 pytest插件豐富,比如flask插件,可用于用例出錯重跑;還有xdist插件,可用于設(shè)備并行執(zhí)行。
二、用例前置和后置
1.unittest提供了setUp/tearDown,只能針對所有用例。
<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> 2.pytest提供了模塊級、函數(shù)級、類級、方法級的setup/teardown,比unittest的setUp/tearDown更靈活。
</pre>
模塊級(setup_module/teardown_module)開始于模塊始末,全局的
函數(shù)級(setup_function/teardown_function)只對函數(shù)用例生效(不在類中)
類級(setup_class/teardown_class)只在類中前后運行一次(在類中)
方法級(setup_method/teardown_method)開始于方法始末(在類中)
類里面的(setup/teardown)運行在調(diào)用方法的前后
<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; position: relative; font-family: Consolas, Inconsolata, Courier, monospace; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> pytest還可以在函數(shù)前加@pytest.fixture()裝飾器,在測試用例中裝在fixture函數(shù)。fixture的使用范圍可以是function,module,class,session。
firture相對于setup和teardown來說有以下幾點優(yōu)勢:</pre>
- 命名方式靈活,不局限于setup和teardown這幾個命名
- conftest.py 配置里可以實現(xiàn)數(shù)據(jù)共享,不需要import就能自動找到一些配置,可供多個py文件調(diào)用。
- scope="module" 可以實現(xiàn)多個.py跨文件共享前置
- scope="session" 以實現(xiàn)多個.py跨文件使用一個session來完成多個用例
- 用yield來喚醒teardown的執(zhí)行
三、斷言
1.unittest提供了assertEqual、assertIn、assertTrue、assertFalse。
2.pytest直接使用assert 表達式。
四、報告
1.unittest使用HTMLTestRunnerNew庫。
2.pytest有pytest-HTML、allure插件。
五、失敗重跑
1、unittest無此功能。
2、pytest支持用例執(zhí)行失敗重跑,pytest-rerunfailures插件。
六、參數(shù)化
1、unittest需依賴ddt庫,
2、pytest直接使用@pytest.mark.parametrize裝飾器。
七、用例分類執(zhí)行
1、unittest默認(rèn)執(zhí)行全部用例,也可以通過加載testsuit,執(zhí)行部分用例。
2、pytest可以通過@pytest.mark來標(biāo)記類和方法,pytest.main加入?yún)?shù)("-m")可以只運行標(biāo)記的類和方法。