Best Python code snippet using pytest-play_python
tic_tac_toe.py
Source:tic_tac_toe.py  
...16    'e'|'o'|'x'17    ---+---+---18    'e'|'e'|'e'19Example:20    This module has a test_play(), which executes21        1. prepare an empty board,22        2. play one hand with `o' randomly,23        2. play one hand with `c' randomly.24    case 1:25        % python26        > import tic_tac_toe27        > tic_tac_toe.test_play()28    case 2:29        % python30        > from tic_tac_toe import *31        > test_play()32    case 3:33        % python tic_tac_toe.py34"""35from __future__ import print_function36import random37def init_board():38    """Return the empty board.39    Args:40        None41    Returns:42        list: the empty board with 9 items.43    >>> board = init_board()44    >>> print(board)45    ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e']46    """47    board = []48    index = 049    while (index <= 9):50        board.append('e')51        index += 152    return board53def print_board(board):54    """Print the board with 3x3.55    Args:56        board (list): the list with 9 items.57    Returns:58        None (only print the board to sys.stdout (standard output)59    >>> board = init_board()60    >>> print_board(board)61    eee62    eee63    eee64    ---65    """66    count = 067    for state in board:68        count += 169        if count % 3 == 0:70            print('{0}'.format(state))71        else:72            print('{0}'.format(state), end='')73    print('---')74def gather_empty_cells(board):75    """Gather empty cells on the board.76    Args:77        board (list): the list with 9 items.78    Returns:79        list: the index list of empty state on the board.80    >>> board = ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'e']81    >>> empty_list = gather_empty_cells(board)82    >>> len(empty_list) == 183    True84    >>> empty_list[0] == 885    True86    """87    empty_list = []88    index = 089    for state in board:90        if state == 'e':91            empty_list.append(index)92        index += 193    return empty_list94def point_random(board, player):95    """Play one step randomly.96    Args:97        board (list): the list with 9 items.98        player (str): 'o' for circle player, 'x' for cross player99    Returns:100        point (int): the pointed index on the board101    Note:102        This function DOESN'T check the validity of turn.103        You can play oneside-game.104    >>> board = init_board()105    >>> index = point_random(board, 'o')106    >>> board[index] == 'o'107    True108    """109    empty_list = gather_empty_cells(board)110    point = random.randint(0, len(empty_list)-1)111    board[empty_list[point]] = player112    return empty_list[point]113def test_play():114    board = init_board()115    print('# board was initialized.')116    print_board(board)117    index = point_random(board, 'o')118    print("# user 'o' pointed to board[{0}]".format(index))119    print_board(board)120    index = point_random(board, 'x')121    print("# user 'x' pointed to board[{0}]".format(index))122    print_board(board)123if __name__ == '__main__':...test_lottery_manager.py
Source:test_lottery_manager.py  
1from faker import Faker2from .dao_test import DaoTest3class TestLotteryManager(DaoTest):4    faker = Faker()5    @classmethod6    def setUpClass(cls):7        super(TestLotteryManager, cls).setUpClass()8        from tests.models.test_play import TestPlay9        cls.test_play = TestPlay10        from mib.dao import lottery_manager11        cls.lottery_manager = lottery_manager.LotteryManager12    def test_crud(self):13        for _ in range(0, 10):14            play = self.test_play.generate_random_play()15            self.lottery_manager.create_lottery_play(lottery=play)16            play1 = self.lottery_manager.retrieve_by_id(play.id)17            self.test_play.assertPlayEquals(play1, play)18            play.set_number(self.faker.random_int(1,100))19            self.lottery_manager.update_lottery_play(lottery=play)20            play1 = self.lottery_manager.retrieve_by_id(play.id)21            self.test_play.assertPlayEquals(play1, play)22            self.lottery_manager.delete_lottery_play(lottery=play)23    def test_retried_by_number(self):24        base_play = self.test_play.generate_random_play()25        self.lottery_manager.create_lottery_play(lottery=base_play)26        retrieved_play = self.lottery_manager.retrieve_by_number(lottery_number=base_play.lottery_number)...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!!
