pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用
1. fixture的聲明
我們使用@pytest.fixture()
來聲明fixture函數(shù)。fixture()
即可無(wú)參數(shù)進(jìn)行聲明,也可以帶參數(shù)聲明。
示例1:
@pytest.fixture()
無(wú)參數(shù)進(jìn)行聲明
import pytest @pytest.fixture #fixture()未帶任何參數(shù),聲明一個(gè)fixture函數(shù) def fixture_demo(): print("這個(gè)是一個(gè)fixture的demo演示") def test_demo(fixture_demo): #調(diào)用fixture函數(shù)——fixture_demo print("這是一個(gè)測(cè)試demo。")
@pytest.fixture()
有參數(shù)的進(jìn)行聲明
通過上文pytest中fixtureAPI簡(jiǎn)單說明,我們對(duì)fixture()
的參數(shù)有了一定了解。fixture()
可以帶著這些參數(shù)去聲明一個(gè)fixture函數(shù)。
import pytest @pytest.fixture(params=[1,2,3]) #fixture()帶著parmas對(duì)ids()進(jìn)行fixture函數(shù)的聲明 def ids(request): data=request.param print(f'獲取測(cè)試數(shù)據(jù){data}') return data def test_ids(ids): #調(diào)用fixture函數(shù)-ids() print(ids)
2. fixture的調(diào)用
2.1 fixture的調(diào)用方式
fixture有三種調(diào)用方式,分別為:
1. 使用 fixturename
2. 使用@pytest.mark.usefixtures(“fixturename”)
3. autouse——自動(dòng)應(yīng)用
2.1.1 使用fixturename
通過pytest中fixtureAPI簡(jiǎn)單說明中對(duì)@fixture()
參數(shù)的介紹,我們知道fixturename默認(rèn)是@pytest.fixture()
所裝飾的函數(shù)的函數(shù)名,如果傳入name
參數(shù),則fixturename就是name
傳入的內(nèi)容。
當(dāng)要調(diào)用fixture的時(shí)候,只要將fixturename作為參數(shù)傳入測(cè)試函數(shù)即可。
示例2:
1.使用被裝飾的函數(shù)的函數(shù)名
import pytest @pytest.fixture() #未傳入name,因此fixturename為函數(shù)名login def login(): print('login') def test_case1(login): #將fixturename作為參數(shù)傳入 print('這是testcase1')
2.使用fixture別名
import pytest @pytest.fixture(name='login1') #傳入name,因此fixturename為login1 def login(): print('login') def test_case1(login1): #將fixturename作為參數(shù)傳入 print('這是testcase1')
注意:
當(dāng)使用fixture的別名后,被裝束函數(shù)的函數(shù)名失效,無(wú)法再繼續(xù)使用其進(jìn)行fixture的調(diào)用。
2.1.2 使用@pytest.mark.usefixtures("fixturename")
根據(jù)pytest官方文檔的介紹,如果想讓某個(gè)fixture適用于類、模塊和項(xiàng)目中所有測(cè)試函數(shù)的話,會(huì)使用usefixtures
來調(diào)用fixture。當(dāng)然,單個(gè)測(cè)試函數(shù)也可以使用此方法來調(diào)用fixture。
使用usefixtures
調(diào)用fixture和直接使用fixturename
來調(diào)用fixture是有區(qū)別的,可總結(jié)為:
使用usefixtures
調(diào)用的fixture只能用于配置測(cè)試前系統(tǒng)的初始狀態(tài),無(wú)法為測(cè)試用例提供測(cè)試數(shù)據(jù);但使用fixturename
調(diào)用的fixture卻可以實(shí)現(xiàn)這兩個(gè)功能。
示例:
1.聲明一個(gè)名為login
的fixture,并通過fixture()
傳入測(cè)試數(shù)據(jù)集。test_case1使用usefixtures
的方法調(diào)用login
,并且在函數(shù)內(nèi)部想要打印login的返回值。
import pytest @pytest.fixture(params=[1,2,3]) # 傳入測(cè)試數(shù)據(jù)集 def login(request): print("登陸操作") return request.param #返回測(cè)試數(shù)據(jù) class TestClass1: @pytest.mark.usefixtures('login') #使用usefixtures調(diào)用login def test_case1(self): print("這是Testclass1中testcase1") print(login) #打印login
運(yùn)行后發(fā)現(xiàn),結(jié)果和我們預(yù)期的不一樣,print(login)
打印出的是函數(shù)對(duì)象信息,不是返回值。
2.修改剛剛的代碼,使用fixturename來調(diào)用login
import pytest @pytest.fixture(params=[1,2,3]) # 傳入測(cè)試數(shù)據(jù)集 def login(request): print("登陸操作") return request.param #返回測(cè)試數(shù)據(jù) class TestClass1: #@pytest.mark.usefixtures('login') #使用usefixtures調(diào)用login def test_case1(self,login): #使用fixturename的方式調(diào)用login print("這是Testclass1中testcase1") print(login) #打印login
運(yùn)行后我們可以發(fā)現(xiàn),結(jié)果和我們預(yù)期的一致,把login的返回值成功打印出來。
usefixtures
調(diào)用fixture的方法只適用于測(cè)試函數(shù),對(duì)于fixture函數(shù)不生效;使用fixturename
調(diào)用fixture的方法對(duì)測(cè)試函數(shù)和fixture函數(shù)都適用。
示例:
1.以下demo想要實(shí)現(xiàn)執(zhí)行測(cè)試用例前打印“登陸操作”,用例執(zhí)行結(jié)束后打印“注銷操作”。聲明login
和logout
為fixture函數(shù),并且讓logout
使用usefixtures
的方法調(diào)用login
,再讓test_case1
調(diào)用logout
。
import pytest @pytest.fixture() def login(): print("登陸操作") @pytest.mark.usefixtures('login') @pytest.fixture() def logout(): yield print("注銷操作") class TestClass1: def test_case1(self,logout): print("這是Testclass1中testcase1")
通過運(yùn)行結(jié)果我們發(fā)現(xiàn),結(jié)果只在執(zhí)行測(cè)試函用例后打印了“注銷操作”,與我們預(yù)期的結(jié)果不一致。
2.修改上面的demo代碼,讓logout
使用fixturename
的方式調(diào)用login
。
import pytest @pytest.fixture() def login(): print("登陸操作") #@pytest.mark.usefixtures('login') @pytest.fixture() def logout(login): #使用fixturename的方式調(diào)用login yield print("注銷操作") class TestClass1: def test_case1(self,logout): print("這是Testclass1中testcase1")
運(yùn)行后我們發(fā)現(xiàn),結(jié)果與我們預(yù)期的一致。
由此可以看出來,userfixtures
的調(diào)用方法對(duì)于fixture函數(shù)無(wú)效。
下面我們將通過示例來演示userfixtures的使用方法:
為了演示效果,我們新建一個(gè)包,并在里面創(chuàng)建一個(gè)conftest.py。用于定義fixture函數(shù)。關(guān)于conftest.py的相關(guān)內(nèi)容,后面會(huì)單獨(dú)寫一遍文章詳細(xì)介紹。
在conftest.py中聲明的一個(gè)login
函數(shù)。
import pytest @pytest.fixture() def login(): print('登錄操作')
1.整個(gè)類中應(yīng)用fixture
使用usefixtures
,讓TestClass1
這個(gè)類中所有測(cè)試函數(shù)都調(diào)用login
。
import pytest @pytest.mark.usefixtures('login') #讓TestClass1調(diào)用login class TestClass1: def test_case1(self): print("這是Testclass1中testcase1") def test_case2(self): print('這是Testclass1中testcase2') class TestClass2: def test_case3(self): print('這是Testclass2中的testcase3')
運(yùn)行結(jié)果:
通過結(jié)果我們可以發(fā)現(xiàn),只有TestClass1中的test_case1和test_case2調(diào)用了login。
2.整個(gè)模塊應(yīng)用fixture
根據(jù)官方文檔說明,在整個(gè)模塊中應(yīng)用fixture,可在模塊使用
pytestmark=pytest.mark.usefixtures("fixturename")。
修改測(cè)試代碼如下:
import pytest pytestmark = pytest.mark.usefixtures("login") #使用pytestmark在模塊中使用usefixtures class TestClass1: def test_case1(self): print("這是Testclass1中testcase1") def test_case2(self): print('這是Testclass1中testcase2') class TestClass2: def test_case3(self): print('這是Testclass2中的testcase3')
運(yùn)行結(jié)果:
通過運(yùn)行結(jié)果可發(fā)現(xiàn),整個(gè)模塊中,所有測(cè)試類里面的測(cè)試函數(shù)都調(diào)用了login。
3.整個(gè)項(xiàng)目中使用
在pytest.ini
中配置usefixtures
。
演示項(xiàng)目結(jié)構(gòu)如圖:
對(duì)應(yīng)文件中的代碼如下:
test_demo
中的test_demo.py
:
class TestClass1: def test_case1(self): print("這是pytest_demo包中test_demo模塊里Testclass1中testcase1") class TestClass2: def test_case2(self): print('這是pytest_demo包中test_demo模塊里Testclass2中的testcase2')
test_usefixtures
中的test_usefixtures.py
:
class TestClass1: def test_case1(self): print("這是pytest_usefixtures包中test_usefixtures模塊里Testclass1中testcase1") class TestClass2: def test_case2(self): print('這是pytest_usefixtures包中test_usefixtures模塊里Testclass2中的testcase2')
TestDemo根目錄
下的conftest.py
:
import pytest @pytest.fixture() def login(): print('登錄操作')
TestDemo根目錄
下的pytest.ini
:
[pytest] usefixtures = login
運(yùn)行整個(gè)項(xiàng)目:
項(xiàng)目中的測(cè)試函數(shù)都調(diào)用了conftest.py中的login。
4.使用usefixtures調(diào)用多個(gè)fixture
我們可以使用@pytest.mark.usefixtures('fixturename1','fixturename2')來調(diào)用多個(gè)fixture。
conftest.py
中新增一個(gè)fixture:
import pytest @pytest.fixture() def login(): print('登錄操作') @pytest.fixture() def printids(): print("打印ids。")
修改test_usefixtures.py
:
@pytest.mark.usefixtures('login','printids') class TestClass1: def test_case1(self): print("這是pytest_usefixtures包中test_usefixtures模塊里Testclass1中testcase1")
運(yùn)行后結(jié)果:
test_case1測(cè)試函數(shù)調(diào)用login
和printids
兩個(gè)fixture。
2.1.3 autouse——自動(dòng)應(yīng)用
fixture()
的autouse
參數(shù),是fixture自動(dòng)應(yīng)用的標(biāo)識(shí)。
如果autouse=True
,則在同作用域下的測(cè)試函數(shù),會(huì)自動(dòng)調(diào)用該fixture;如果autouse=False
,則測(cè)試函數(shù)需要主動(dòng)去調(diào)用該fixture。
autouse
默認(rèn)是False
。
示例4:
1.在TestClass1
這個(gè)類中定義了一個(gè)login
函數(shù),聲明為fixture,并且autouse
設(shè)置為True
。
TestClass1
中的test_case1
主動(dòng)調(diào)用了login
,但是其他測(cè)試函數(shù)未主動(dòng)調(diào)用。
我們還寫了一個(gè)TestClass2
類,類中有一個(gè)test_case4
的測(cè)試函數(shù)。
import pytest class TestClass1: @pytest.fixture(autouse=True) # 啟用自動(dòng)應(yīng)用 def login(self): print("登陸操作") def test_case1(self,login): # 調(diào)用了login這個(gè)fixture函數(shù) print("這是Testclass1中testcase1") def test_case2(self): # 沒有調(diào)用 print('這是Testclass1中testcase2') def test_case3(self): # 沒有調(diào)用 print('這是Testclass1中testcase3') class TestClass2: def test_case4(self): print('這是Testclass2中的testcase4')
運(yùn)行結(jié)果:
通過運(yùn)行結(jié)果我們可以知道,當(dāng)fixture的autouse=True
的時(shí)候,在同作用域內(nèi)的測(cè)試函數(shù)會(huì)自動(dòng)調(diào)用fixture,非同作用域內(nèi)的測(cè)試函數(shù)無(wú)法調(diào)用。
2.我們給fixture()
帶上更多的參數(shù),修改上面的demo,并設(shè)置fixture的scope=‘class'
。
import pytest @pytest.fixture(scope='class', autouse=True) # fixture的作用域設(shè)為class級(jí)別,啟用自動(dòng)應(yīng)用 def login(): print("登陸操作") class TestClass1: def test_case1(self): print("這是Testclass1中testcase1") def test_case2(self): print('這是Testclass1中testcase2') def test_case3(self): print('這是Testclass1中testcase3') class TestClass2: def test_case4(self): print('這是Testclass2中的testcase4')
運(yùn)行結(jié)果:
通過運(yùn)行結(jié)果我們可以看出,當(dāng)設(shè)置login
的scope=‘class'
的使用,每一個(gè)測(cè)試類都會(huì)自動(dòng)調(diào)用一次login
。
autouse
的使用也是遵照fixture函數(shù)的設(shè)置來進(jìn)行的。
2.2 fixture使用的靈活性
通過官方文檔的說明,我們知道了pytest的fixture系統(tǒng)是極其的靈活和強(qiáng)大的,官方文檔也為我們以下幾個(gè)靈活使用fixture的例子。
2.2.1 一個(gè)fixture函數(shù)可以調(diào)用其他的fixture
文章前面我們有演示過一個(gè)fixture函數(shù)調(diào)用其他fixture的例子。
代碼如下:
import pytest @pytest.fixture() def login(): print("登陸操作") @pytest.fixture() def logout(login): yield print("注銷操作") class TestClass1: def test_case1(self,logout): print("這是Testclass1中testcase1")
login()
實(shí)現(xiàn)了測(cè)試執(zhí)行的前置操作,logout()
實(shí)現(xiàn)了測(cè)試執(zhí)行的后置操作。logout()
調(diào)用了login
,測(cè)試函數(shù)則直接調(diào)用了logout
。
fixture在執(zhí)行的時(shí)候是依次執(zhí)行的。假如fixture A 調(diào)用了fixture B,那么執(zhí)行的時(shí)候會(huì)先執(zhí)行fixture B,然后再執(zhí)行fixture A。因?yàn)閒ixture B是fixture A的依賴條件。
所以我們運(yùn)行上面代碼的時(shí)候,是會(huì)先調(diào)用login()
然后再調(diào)用logout(
)的。而logout()
中的yield
會(huì)讓測(cè)試用例執(zhí)行的時(shí)候先執(zhí)行login
中的測(cè)試前置操作,然后再執(zhí)行測(cè)試用例中的內(nèi)容,最后執(zhí)行logout
中yield
后面測(cè)試后置操作。
這樣的一個(gè)操作,可以將復(fù)雜的測(cè)試需求,變成一個(gè)一個(gè)簡(jiǎn)單而又有組織性的的功能函數(shù),更加方便后期進(jìn)行維護(hù)。
2.2.2 fixture函數(shù)可被反復(fù)重用
兩個(gè)不同的測(cè)試函數(shù)可以調(diào)用同一個(gè)fixture,并且獲得各自的運(yùn)行結(jié)果。測(cè)試函數(shù)之間并不會(huì)因?yàn)檎{(diào)用了同一個(gè)fixture而相互之間產(chǎn)生任何影響。
示例5:
演示代碼:
import pytest @pytest.fixture() def first_number(): #fixture函數(shù),返回1 return 1 @pytest.fixture() def order(first_number): #fixture函數(shù),調(diào)用了first_number這個(gè)fixture,并且返回一個(gè)list return [first_number] def test_secondnum1(order): #調(diào)用order,并且在order返回的list中添加 2 這個(gè)元素 order.append(2) print(order) assert order==[1,2] #斷言 def test_secondnum2(order): #也調(diào)用了order,并在order返回的list中添加 3 這個(gè)元素 order.append(3) print(order) assert order==[1,3] #斷言
運(yùn)行結(jié)果:
上面這段代碼我們聲明了:
兩個(gè)fixture——first_number
和order
。
first_number
有一個(gè)返回值: 1
;
order
調(diào)用了first_number
,并返回一個(gè)列表: [1]
。
我們還定義了:
兩個(gè)測(cè)試函數(shù)——test_secondnum1
和test_secondnum2
,這兩個(gè)測(cè)試函數(shù)都調(diào)用了order
。
test_secondnum1
對(duì)order
的返回值做了一個(gè)append(2)
的操作;test_secondnum1
對(duì)order
的返回值做了一個(gè)append(3)
的操作。
根據(jù)運(yùn)行結(jié)果我們可以看出,兩個(gè)測(cè)試函數(shù)對(duì)調(diào)用的fixture都做出了各自的操作,并且得到各自的一個(gè)運(yùn)行結(jié)果。兩者的運(yùn)行結(jié)果沒有因?yàn)槎颊{(diào)用了order
而相互產(chǎn)生影響。
fixture可被反復(fù)重用這一特點(diǎn),對(duì)于確保測(cè)試之間彼此不受影響是非常有用的。我們可以聲明一個(gè)通用的fixture函數(shù),并使用這個(gè)特性來確保每個(gè)測(cè)試都能得到未受污染的測(cè)試數(shù)據(jù),并且能從一個(gè)干凈的初始狀態(tài)開始執(zhí)行。
2.2.3 測(cè)試函數(shù)/fixture函數(shù)可以一次調(diào)用多個(gè)fixture
文章前面我們有介紹過如何使用usefixtures來調(diào)用多個(gè)fixture,由此我們可以知道,pytest是允許測(cè)試函數(shù)和fixture函數(shù)按照自己的需求一次調(diào)用多個(gè)fixture的。
示例6:
演示代碼:
import pytest @pytest.fixture() def first_number(): #第一個(gè)fixture,返回1 return 1 @pytest.fixture() def second_number(): #第二個(gè)fixture,返回2 return 2 @pytest.fixture() def third_number(): #第三個(gè)fixture,返回1 return 3 @pytest.fixture() def order(first_number,second_number): #調(diào)用first_number 和 second_number 返回 [1,2] return [first_number,second_number] def test_case(order,third_number): #調(diào)用order 和 third_number order.append(third_number) #對(duì) order做append的操作,將third_number的返回值加入list中 print(order) assert order==[1,2,3] #斷言
運(yùn)行結(jié)果:
演示代碼中我們聲明了:
四個(gè)fixture——first_number
、second_number
、third_number
和order
;
一個(gè)測(cè)試函數(shù)——test_case
。
order
調(diào)用了first_number
和second_number
;test_case
調(diào)用了order
和third_number
。他們都根據(jù)自己的需求調(diào)用了多個(gè)fixture,并且正常被執(zhí)行。
2.2.4 同一測(cè)試執(zhí)行期間,fixture可被多次請(qǐng)求
其實(shí)在看官方文檔的時(shí)候,我有點(diǎn)不太理解這部分的內(nèi)容。當(dāng)時(shí)看文字的描述感覺與“fixture能夠被重復(fù)調(diào)用“這一點(diǎn)有沖突,但是通過官方文檔的代碼示例,我才明白這兩點(diǎn)其實(shí)是不沖突的。
下面我們先看一下官方示例。
示例7:
演示代碼:
import pytest @pytest.fixture def first_entry(): return "a" @pytest.fixture def order(): return [] @pytest.fixture def append_first(order, first_entry): return order.append(first_entry) def test_string_only(append_first,order, first_entry): print(order) assert order == [first_entry]
運(yùn)行結(jié)果:
示例代碼中聲明了:
三個(gè)fixture函數(shù)——first_entry
、order
和append_first
;
一個(gè)測(cè)試函數(shù)——test_string_only
。
first_entry
返回了一個(gè)str: "a"
;
order
返回一個(gè)空l(shuí)ist:[]
;
append_first
調(diào)用了first_entry
和order
,并且返回 order.append(first_entry)
。
test_string_only
調(diào)用了first_entry
、order
和append_first
,并做了一個(gè)打印order
值的操作和斷言操作。
通過運(yùn)行結(jié)果我們可以看到,test_string_only
斷言成功,并且打印的order
的值是['a']
。
如果按照”fixture能夠被重復(fù)調(diào)用“這一特點(diǎn)看,打印的order
不應(yīng)該是空l(shuí)ist的嗎?為什么會(huì)是[‘a(chǎn)']呢?
test_string_only
在執(zhí)行期間,先執(zhí)行append_first
,而append_first
調(diào)用了order
,并且對(duì)order
進(jìn)行了添加元素的操作,更改了order
的值,此時(shí)order
返回的值會(huì)存在緩存中。當(dāng)test_string_only
后面再去”調(diào)用“order
的時(shí)候,其實(shí)和append_first
引用的是同一個(gè)order對(duì)象,因此測(cè)試斷言才會(huì)成功。
通過pytest官方文檔我們知道,pytest一次只緩存一個(gè)fixture的實(shí)例,這意味著在使用參數(shù)化fixture時(shí),pytest可以在給定范圍內(nèi)多次調(diào)用一個(gè)fixture。
當(dāng)測(cè)試過程中先調(diào)用的fixture對(duì)其他fixture有依賴關(guān)系的話,在調(diào)用這個(gè)fixture的時(shí)候它所依賴的fixture也會(huì)被調(diào)用。所以后面測(cè)試執(zhí)行過程中,如果再次有請(qǐng)求到這些fixture的話,fixture就不會(huì)被再次執(zhí)行,此時(shí)會(huì)直接從緩存中獲取到之前fixture執(zhí)行后返回的數(shù)據(jù)來使用。
“fixture能夠被重復(fù)調(diào)用“——針對(duì)不同的測(cè)試過程,目的是為了保障每個(gè)測(cè)試執(zhí)行時(shí)都是一個(gè)干凈的環(huán)境。
“同一測(cè)試執(zhí)行期間,fixture可被多次請(qǐng)求”——同一測(cè)試過程中,目的是為了保障在測(cè)試過程中,前后使用的數(shù)據(jù)不會(huì)被重置。
文末說明:
以上內(nèi)容是我在閱讀pytest官方文檔后,依照個(gè)人理解進(jìn)行整理。內(nèi)容可能會(huì)有理解錯(cuò)誤之處,歡迎大家留言指正。謝謝
以上就是pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用的詳細(xì)內(nèi)容,更多關(guān)于fixture聲明和調(diào)用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)戰(zhàn)項(xiàng)目刮刮樂的實(shí)現(xiàn)詳解流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python實(shí)現(xiàn)一個(gè)刮刮樂的小項(xiàng)目,大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11Python有關(guān)Unicode UTF-8 GBK編碼問題詳解
本文主要介紹了Python有關(guān)Unicode UTF-8 GBK編碼問題詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼
本文主要介紹了Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python中print函數(shù)簡(jiǎn)單使用總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Python中怎么使用print函數(shù)的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2019-08-08Python實(shí)戰(zhàn)之基于OpenCV的美顏掛件制作
在本文中,我們將學(xué)習(xí)如何創(chuàng)建有趣的基于Snapchat的增強(qiáng)現(xiàn)實(shí),主要包括兩個(gè)實(shí)戰(zhàn)項(xiàng)目:在檢測(cè)到的人臉上的鼻子和嘴巴之間添加胡子掛件,在檢測(cè)到的人臉上添加眼鏡掛件。感興趣的童鞋可以看看哦2021-11-11Python實(shí)現(xiàn)中文文本關(guān)鍵詞抽取的三種方法
文本關(guān)鍵詞抽取,是對(duì)文本信息進(jìn)行高度凝練的一種有效手段,通過3-5個(gè)詞語(yǔ)準(zhǔn)確概括文本的主題,幫助讀者快速理解文本信息,本文分別采用TF-IDF方法、TextRank方法和Word2Vec詞聚類方法,利用Python語(yǔ)言進(jìn)行開發(fā),實(shí)現(xiàn)文本關(guān)鍵詞的抽取,需要的朋友可以參考下2024-01-01