How to use my_fixture method in Pytest

Best Python code snippet using pytest

bbc_fac_livescore.py

Source:bbc_fac_livescore.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

test_fixture.py

Source:test_fixture.py Github

copy

Full Screen

...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): ...

Full Screen

Full Screen

livescore.py

Source:livescore.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

pytest.py

Source:pytest.py Github

copy

Full Screen

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']...

Full Screen

Full Screen

Pytest Tutorial

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.

Chapters

  1. What is pytest
  2. Pytest installation: Want to start pytest from scratch? See how to install and configure pytest for Python automation testing.
  3. Run first test with pytest framework: Follow this step-by-step tutorial to write and run your first pytest script.
  4. Parallel testing with pytest: A hands-on guide to parallel testing with pytest to improve the scalability of your test automation.
  5. Generate pytest reports: Reports make it easier to understand the results of pytest-based test runs. Learn how to generate pytest reports.
  6. Pytest Parameterized tests: Create and run your pytest scripts while avoiding code duplication and increasing test coverage with parameterization.
  7. Pytest Fixtures: Check out how to implement pytest fixtures for your end-to-end testing needs.
  8. Execute Multiple Test Cases: Explore different scenarios for running multiple test cases in pytest from a single file.
  9. Stop Test Suite after N Test Failures: See how to stop your test suite after n test failures in pytest using the @pytest.mark.incremental decorator and maxfail command-line option.

YouTube

Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.

https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP

Run Pytest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful