Best Python code snippet using pytest
bbc_fac_livescore.py
Source:bbc_fac_livescore.py
1from bs4 import BeautifulSoup2from urllib.request import urlopen3import requests4from datetime import datetime5import time6from .livescore import Livescore7class BBC_FAC_Livescore(Livescore):8 ID = "BBC_FAC"9 NAME = "BBC English FA Cup"10 def __init__(self, team):11 self.team = team12 def get_live_score(self):13 14 self.LIVESCORE_URL = 'https://www.bbc.com/sport/football/fa-cup/scores-fixtures'15 self.now = datetime.now() # current date and time16 self.url = self.LIVESCORE_URL + '?' + self.now.strftime("%Y%m%d%h%M%s")17 self.page = requests.get(self.url)18 self.soup = BeautifulSoup(self.page.text, "lxml")19 self.my_fixture = {}20 self.my_fixture["team-home"] = 'default'21 self.my_fixture["team-away"] = 'default'22 self.my_fixture["score-home"] = 023 self.my_fixture["score-away"] = 024 self.my_fixture["status"] = 'No Game'25 self.my_fixture["start-time"] = ""26 self.all_fixtures = self.soup.find_all("li", class_="gs-o-list-ui__item gs-u-pb-") 27 if self.all_fixtures:28 for self.fixture in self.all_fixtures:29 if self.team in self.fixture.text:30 if self.fixture.find("span", class_="sp-c-fixture__team-name--home"): # Started31 self.my_fixture["team-home"] = self.fixture.find("span", class_="sp-c-fixture__team-name--home") \32 .find("span", class_="qa-full-team-name").text33 self.my_fixture["team-away"] = self.fixture.find("span", class_="sp-c-fixture__team-name--away") \34 .find("span", class_="qa-full-team-name").text35 self.my_fixture["score-home"] = self.fixture.find("span", class_="sp-c-fixture__number--home").text36 self.my_fixture["score-away"] = self.fixture.find("span", class_="sp-c-fixture__number--away").text37 if self.fixture.find("span", class_="sp-c-fixture__status"):38 self.my_fixture["status"] = self.fixture.find("span", class_="sp-c-fixture__status").text.split(" ",1)[0] + "'"39 else:40 self.my_fixture["status"] = ""41 if self.fixture.find("span", class_="sp-c-fixture__number--time"):42 self.my_fixture["start-time"] = self.fixture.find("span", class_="sp-c-fixture__number--time").text43 else:44 self.my_fixture["start-time"] = ""45 46 return self.my_fixture47 else:48 self.my_fixture["team-home"] = self.fixture.find("span", class_="sp-c-fixture__team--time-home") \49 .find("span", class_="qa-full-team-name").text50 self.my_fixture["team-away"] = self.fixture.find("span", class_="sp-c-fixture__team--time-away") \51 .find("span", class_="qa-full-team-name").text52 #self.my_fixture["score-home"] = self.fixture.find("span", class_="sp-c-fixture__number--home").text53 #self.my_fixture["score-away"] = self.fixture.find("span", class_="sp-c-fixture__number--away").text54 if self.fixture.find("span", class_="sp-c-fixture__status"):55 self.my_fixture["status"] = self.fixture.find("span", class_="sp-c-fixture__status").text.split(" ",1)[0] + "'"56 else:57 self.my_fixture["status"] = ""58 if self.fixture.find("span", class_="sp-c-fixture__number--time"):59 self.my_fixture["start-time"] = self.fixture.find("span", class_="sp-c-fixture__number--time").text60 else:61 self.my_fixture["start-time"] = ""62 63 return self.my_fixture64 65 return self.my_fixture66 def get_all_fixtures(self):67 68 self.LIVESCORE_URL = 'https://www.bbc.co.uk/sport/football/fa-cup/scores-fixtures/'69 70 self.now = datetime.now() # current date and time71 self.url = self.LIVESCORE_URL + '?' + self.now.strftime("%Y-%m-%d")72 self.page = requests.get(self.url)73 self.soup = BeautifulSoup(self.page.text, "lxml")74 self.all_my_fixtures = []75 self.all_fixtures = self.soup.find_all("li", class_="gs-o-list-ui__item gs-u-pb-") 76 if self.all_fixtures:77 for self.fixture in self.all_fixtures:78 my_fixture = {}79 if self.fixture.find("span", class_="sp-c-fixture__team-name--home"):80 my_fixture["team-home"] = self.fixture.find("span", class_="sp-c-fixture__team-name--home") \81 .find("span", class_="qa-full-team-name").text82 my_fixture["team-away"] = self.fixture.find("span", class_="sp-c-fixture__team-name--away") \83 .find("span", class_="qa-full-team-name").text84 my_fixture["score-home"] = self.fixture.find("span", class_="sp-c-fixture__number--home").text85 my_fixture["score-away"] = self.fixture.find("span", class_="sp-c-fixture__number--away").text86 if self.fixture.find("span", class_="sp-c-fixture__status"):87 my_fixture["status"] = self.fixture.find("span", class_="sp-c-fixture__status").text.replace('Extra Time ', '').split(" ",1)[0]88 my_fixture["status"] = my_fixture["status"] + ("'" if my_fixture["status"].isnumeric() else "")89 else:90 my_fixture["status"] = ""91 if self.fixture.find("span", class_="sp-c-fixture__number--time"):92 my_fixture["start-time"] = self.fixture.find("span", class_="sp-c-fixture__number--time").text93 else:94 my_fixture["start-time"] = ""95 96 self.all_my_fixtures.append(my_fixture)97 else: # Not started98 my_fixture["team-home"] = self.fixture.find("span", class_="sp-c-fixture__team--time-home") \99 .find("span", class_="qa-full-team-name").text100 my_fixture["team-away"] = self.fixture.find("span", class_="sp-c-fixture__team--time-away") \101 .find("span", class_="qa-full-team-name").text102 my_fixture["score-home"] = "0"103 my_fixture["score-away"] = "0"104 if self.fixture.find("span", class_="sp-c-fixture__status"):105 my_fixture["status"] = self.fixture.find("span", class_="sp-c-fixture__status").text.split(" ",1)[0]106 my_fixture["status"] = my_fixture["status"] + ("'" if my_fixture["status"].isnumeric() else "")107 else:108 my_fixture["status"] = ""109 if self.fixture.find("span", class_="sp-c-fixture__number--time"):110 my_fixture["start-time"] = self.fixture.find("span", class_="sp-c-fixture__number--time").text111 else:112 my_fixture["start-time"] = ""113 114 self.all_my_fixtures.append(my_fixture)...
test_fixture.py
Source:test_fixture.py
...14nameï¼ç»è¡¨ç¤ºçæ¯è¢«@pytest.fixtureæ è®°çæ¹æ³åä¸ä¸ªå«å15'''1617# @pytest.fixture(scope="function")18# def my_fixture():19# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')20# yield21# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')22#23# class Testcase:24#25# def test_001(self):26# print('\n001')27#28# def test_002(self,my_fixture):29# print('\n002')30#31# if __name__ == '__main__':32# pytest.main()333435# @pytest.fixture(scope="class",autouse='Ture')36# def my_fixture():37# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')38# yield39# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')40#41# class Testcase1:42#43# def test_001(self):44# print('\n001')45#46# def test_002(self,my_fixture):47# print('\n002')48#49# class Testcase2:50#51# def test_001(self):52# print('\n003')53#54# def test_002(self,my_fixture):55# print('\n004')56# if __name__ == '__main__':57# pytest.main()5859# @pytest.fixture(scope="module",autouse='Ture')60# def my_fixture():61# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')62# yield63# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')64#65# class Testcase1:66#67# def test_001(self):68# print('\næµè¯001')69#70# def test_002(self,my_fixture):71# print('\næµè¯002')72#73# class Testcase2:74#75# def test_001(self):76# print('\næµè¯003')77#78# def test_002(self,my_fixture):79# print('\næµè¯004')80# if __name__ == '__main__':81# pytest.main()828384# @pytest.fixture(scope="module",params=['æé¾','æå°é¾','long'],ids=['cl','lxl','l'],name='aaaaa')85# # params=['æé¾','æå°é¾','long']çparamsæ¯åæ°åï¼æs86# # idsæ¯å°paramséæ°å½å87# # å½åäºå«ååï¼åæ¥çå称ä¸å¯ä½¿ç¨88# def my_fixture(request):89# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')90# yield request.param91# print('\nè¿æ¯åç½®çæ¹æ³ï¼å¯å®ç°é¨å以åå
¨é¨ç¨ä¾çåç½®')92# # return request.param93# # request.paramæ¯å±æ§åï¼æ²¡æs94# #return å yieldé½è¡¨ç¤ºè¿åï¼ä½return åä¸è½æ代ç ï¼yield è¿åååé¢å¯æ¥ä»£ç 95#96#97# class Testcase1:98#99# def test_001(self):100# print('\næµè¯001')101#102# def test_002(self,aaaaa):
...
livescore.py
Source:livescore.py
1from bs4 import BeautifulSoup2from urllib.request import urlopen3from datetime import datetime4import time5import requests6class Livescore:7 LIVESCORE_URL = ''8 def get_live_score(self):9 pass10 def get_all_fixtures(self):11 self.now = datetime.now() # current date and time12 self.url = self.LIVESCORE_URL + '?' + self.now.strftime("%Y-%m-%d")13 self.page = requests.get(self.url)14 self.soup = BeautifulSoup(self.page.text, "lxml")15 self.all_my_fixtures = []16 self.all_fixtures = self.soup.find_all("li", class_="gs-o-list-ui__item gs-u-pb-") 17 if self.all_fixtures:18 for self.fixture in self.all_fixtures:19 my_fixture = {}20 if self.fixture.find("span", class_="sp-c-fixture__team-name--home"):21 my_fixture["team-home"] = self.fixture.find("span", class_="sp-c-fixture__team-name--home") \22 .find("span", class_="qa-full-team-name").text23 my_fixture["team-away"] = self.fixture.find("span", class_="sp-c-fixture__team-name--away") \24 .find("span", class_="qa-full-team-name").text25 my_fixture["score-home"] = self.fixture.find("span", class_="sp-c-fixture__number--home").text26 my_fixture["score-away"] = self.fixture.find("span", class_="sp-c-fixture__number--away").text27 if self.fixture.find("span", class_="sp-c-fixture__status"):28 my_fixture["status"] = self.fixture.find("span", class_="sp-c-fixture__status").text.replace('Extra Time ', '').split(" ",1)[0]29 my_fixture["status"] = my_fixture["status"] + ("'" if my_fixture["status"].isnumeric() else "")30 else:31 my_fixture["status"] = ""32 if self.fixture.find("span", class_="sp-c-fixture__number--time"):33 my_fixture["start-time"] = self.fixture.find("span", class_="sp-c-fixture__number--time").text34 else:35 my_fixture["start-time"] = ""36 37 self.all_my_fixtures.append(my_fixture)38 else: # Not started39 my_fixture["team-home"] = self.fixture.find("span", class_="sp-c-fixture__team--time-home") \40 .find("span", class_="qa-full-team-name").text41 my_fixture["team-away"] = self.fixture.find("span", class_="sp-c-fixture__team--time-away") \42 .find("span", class_="qa-full-team-name").text43 my_fixture["score-home"] = "0"44 my_fixture["score-away"] = "0"45 if self.fixture.find("span", class_="sp-c-fixture__status"):46 my_fixture["status"] = self.fixture.find("span", class_="sp-c-fixture__status").text.split(" ",1)[0]47 my_fixture["status"] = my_fixture["status"] + ("'" if my_fixture["status"].isnumeric() else "")48 else:49 my_fixture["status"] = ""50 if self.fixture.find("span", class_="sp-c-fixture__number--time"):51 start_time = self.fixture.find("span", class_="sp-c-fixture__number--time").text52 my_fixture["start-time"] = start_time53 else:54 my_fixture["start-time"] = ""55 56 self.all_my_fixtures.append(my_fixture)...
pytest.py
Source:pytest.py
1import pytest2from pytest import fixture3@pytest.fixture(scope='module')4def my_fixture() -> str:5 pass6@fixture7def my_simple_fixture():8 return 19@fixture10def my_yield_fixture():11 yield 112@fixture13class MyClassFixture():14 pass15# -----------------16# goto/infer17# -----------------18#! 18 ['def my_conftest_fixture']...
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!