Best Python code snippet using pytest-mock
ml_backend.py
Source: ml_backend.py
...23 'Pts5', 'OR1', 'OR2', 'OR3', 'OR4', 'OR5', 'DR1', 'DR2', 'DR3',24 'DR4', 'DR5', 'FG2Pct', 'FG3Pct', 'FTPct', 'BlockPct',25 'OppFG2Pct', 'OppFG3Pct', 'OppFTPct', 'OppBlockPct', 'F3GRate',26 'OppF3GRate', 'ARate', 'OppARate', 'StlRate', 'OppStlRate']27def get_random_number():28 """generate random number between -1 and 1."""29 rand = random.random()30 rand = (rand * 2) - 131 return rand32def prepare_data(year1, team1, year2, team2, mongo):33 """Prepare data for prediction."""34 data = []35 team_year_info = mongo.db.basketball.find_one(36 {'TeamName': str(team1), 'Season': int(year1)})37 for col in cols:38 data.append(team_year_info[col])39 tc1 = team_year_info['color1']40 team_year_info = mongo.db.basketball.find_one(41 {'TeamName': str(team2), 'Season': int(year2)})42 for col in cols:43 data.append(team_year_info[col])44 tc2 = team_year_info['color1']45 data.append(home_court.get(str(team1), 0))46 data = np.array(data)47 return data, tc1, tc248def randomize_data(year1, team1, year2, team2, data):49 """randomize data for bootstrap predictions."""50 data_std = year_team_std[year1].get(team1, year_team_std['all'])51 cols_len = len(cols)52 data_copy = data.copy()53 data_copy[0] += get_random_number() * data_std['Pace']54 data_copy[1] += get_random_number() * data_std['ORtg']55 data_copy[2] += get_random_number() * data_std['DRtg']56 data_copy[3] += get_random_number() * data_std['OeFG%'] * 10057 data_copy[4] += get_random_number() * data_std['DeFG%'] * 10058 data_copy[5] += get_random_number() * data_std['OTOV%']59 data_copy[6] += get_random_number() * data_std['DTOV%']60 data_copy[7] += get_random_number() * data_std['OORB%']61 data_copy[8] += get_random_number() * data_std['DDRB%']62 data_copy[9] += get_random_number() * data_std['OFT/FGA'] * 10063 data_copy[10] += get_random_number() * data_std['DFT/FGA'] * 10064 data_copy[42] += get_random_number() * data_std['h3P%'] * 10065 data_copy[43] += get_random_number() * data_std['hFT%'] * 10066 data_copy[44] += get_random_number() * data_std['BLK%']67 data_copy[51] += get_random_number() * data_std['AST%']68 data_copy[53] += get_random_number() * data_std['STL%'] / 10069 data_std = year_team_std[year2].get(team2, year_team_std['all'])70 data_copy[0 + cols_len] += get_random_number() * data_std['Pace']71 data_copy[1 + cols_len] += get_random_number() * data_std['ORtg']72 data_copy[2 + cols_len] += get_random_number() * data_std['DRtg']73 data_copy[3 + cols_len] += get_random_number() * data_std['OeFG%'] * 10074 data_copy[4 + cols_len] += get_random_number() * data_std['DeFG%'] * 10075 data_copy[5 + cols_len] += get_random_number() * data_std['OTOV%']76 data_copy[6 + cols_len] += get_random_number() * data_std['DTOV%']77 data_copy[7 + cols_len] += get_random_number() * data_std['OORB%']78 data_copy[8 + cols_len] += get_random_number() * data_std['DDRB%']79 data_copy[9 + cols_len] += get_random_number() * data_std['OFT/FGA'] * 10080 data_copy[10 + cols_len] += get_random_number() * data_std['DFT/FGA'] * 10081 data_copy[42 + cols_len] += get_random_number() * data_std['h3P%'] * 10082 data_copy[43 + cols_len] += get_random_number() * data_std['hFT%'] * 10083 data_copy[44 + cols_len] += get_random_number() * data_std['BLK%']84 data_copy[51 + cols_len] += get_random_number() * data_std['AST%']85 data_copy[53 + cols_len] += get_random_number() * data_std['STL%'] / 10086 return data_copy87def bootstrap(year1, team1, year2, team2, mongo):88 """predict 100 random games."""89 output = {}90 data, tc1, tc2 = prepare_data(year1, team1, year2, team2, mongo)91 data_df = pd.DataFrame(data.reshape(1, 111))92 data_copy = data.copy()93 num_trials = 9994 for i in range(num_trials):95 rand_data = randomize_data(year1, team1, year2, team2, data_copy)96 data_df.loc[len(data_df)] = rand_data97 data_df -= ml_stats_mean98 data_df = data_df / ml_stats_std99 global graph...
test_cached.py
Source: test_cached.py
2import pytest3import random4import warnings5@cached6def get_random_number(a, b, *args, **kwars):7 print(a, b)8 return random.randint(a, b)9@cached10def get_number(a, b, *args, **kwars):11 return (a + b) // 212class TestCached:13 def initialize(self):14 random.seed(42)15 def test_simple(self):16 self.initialize()17 for i in range(100):18 assert get_random_number(1, 2) == get_random_number(1, 2)19 def test_hashable_types(self):20 self.initialize()21 tpl = (1, 2.0, '123', None, True)22 for i in range(100):23 assert get_random_number(1, 2, tpl) == get_random_number(1, 2, tpl)24 def test_runtime_warning_unhashable_types(self):25 unhashable = {1: 2, 3: 4}26 with pytest.warns(RuntimeWarning):27 get_number(1, 2, unhashable)28 def test_unhashable_types(self):29 warnings.simplefilter("ignore")...
test_fixture.py
Source: test_fixture.py
...16# assert os.path.exists(directory), 'dir not exist'171819@pytest.fixture(scope="session")20def get_random_number():21 number = random.randint(1, 100)22 print(number)23 return number242526def test_random_number(get_random_number):27 assert 0 < get_random_number <= 100282930def test_random_number_2(get_random_number):31 assert 0 < get_random_number <= 100323334class TestRandom:
...
test_2.py
Source: test_2.py
1import pytest2from proga import get_random_number3def test_get_random_number_1():4 assert isinstance(get_random_number(12), int)5def test_get_random_number_2():6 assert get_random_number(12) <= 127def test_get_random_number_3():8 assert isinstance(get_random_number(20), int)9def test_get_random_number_4():10 assert get_random_number(20) <= 2011def test_get_random_number_5():12 assert isinstance(get_random_number(123), int)13def test_get_random_number_6():...
Check out the latest blogs from LambdaTest on this topic:
The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.
In some sense, testing can be more difficult than coding, as validating the efficiency of the test cases (i.e., the ‘goodness’ of your tests) can be much harder than validating code correctness. In practice, the tests are just executed without any validation beyond the pass/fail verdict. On the contrary, the code is (hopefully) always validated by testing. By designing and executing the test cases the result is that some tests have passed, and some others have failed. Testers do not know much about how many bugs remain in the code, nor about their bug-revealing efficiency.
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!