|
| 1 | +# conftest.py |
| 2 | +import pytest |
| 3 | +import random |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +# add a commandline option to pytest |
| 10 | +def pytest_addoption(parser): |
| 11 | + """Add random seed option to py.test. |
| 12 | + """ |
| 13 | + parser.addoption('--seed', dest='seed', type=int, action='store', |
| 14 | + help='set random seed') |
| 15 | + |
| 16 | + |
| 17 | +# configure pytest to automatically set the rnd seed if not passed on CLI |
| 18 | +def pytest_configure(config): |
| 19 | + seed = config.getvalue("seed") |
| 20 | + # if seed was not set by the user, we set one now |
| 21 | + if seed is None or seed == ('NO', 'DEFAULT'): |
| 22 | + config.option.seed = int(np.random.randint(2 ** 31 - 1)) |
| 23 | + |
| 24 | + |
| 25 | +def pytest_report_header(config): |
| 26 | + return f'Using random seed: {config.option.seed}' |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def random_state(request): |
| 31 | + random_state = np.random.RandomState(request.config.option.seed) |
| 32 | + return random_state |
| 33 | + |
| 34 | + |
| 35 | +# conftest.py |
| 36 | +import pytest |
| 37 | +import random |
| 38 | + |
| 39 | +FLASHY_EMOJIS_PASS = ["🎉", "✨", "🌈", "💎", "🔥", "🚀", "💃", "🍾", "🥳"] |
| 40 | +FLASHY_EMOJIS_FAIL = ["💀", "🔥", "💔", "😱", "👹", "🩸", "☠️", "👾", "🤯"] |
| 41 | +FLASHY_EMOJIS_SKIP = ["⏭", "🙃", "🕊", "💤", "📭", "🤸"] |
| 42 | + |
| 43 | +# Vivid ANSI 256-color palette (high contrast, no pale shades) |
| 44 | +VIVID_COLORS = [196, 202, 208, 34, 27, 93, 129, 201] |
| 45 | +# bright red, orange, gold, green, deep blue, violet, magenta, pink |
| 46 | + |
| 47 | +def rainbow_text(text: str) -> str: |
| 48 | + """Return text with vivid rainbow cycling ANSI 256-color codes.""" |
| 49 | + result = [] |
| 50 | + for i, char in enumerate(text): |
| 51 | + color = VIVID_COLORS[i % len(VIVID_COLORS)] |
| 52 | + result.append(f"\033[38;5;{color}m{char}\033[0m") |
| 53 | + return "".join(result) |
| 54 | + |
| 55 | +def big_banner(text: str) -> str: |
| 56 | + """ASCII art style banner with rainbow colors.""" |
| 57 | + border = rainbow_text("=" * (len(text) + 12)) |
| 58 | + middle = rainbow_text(f"🌟✨ {text} ✨🌟") |
| 59 | + return f"\n{border}\n{middle}\n{border}\n" |
| 60 | + |
| 61 | +def pytest_report_teststatus(report, config): |
| 62 | + if report.when == "call": |
| 63 | + if report.failed: |
| 64 | + return report.outcome, "💥", "FAILED FLASHY" |
| 65 | + elif report.passed: |
| 66 | + return report.outcome, "🌟", "PASSED FLASHY" |
| 67 | + elif report.skipped: |
| 68 | + return report.outcome, "⏭", "SKIPPED FLASHY" |
| 69 | + |
| 70 | +@pytest.hookimpl(trylast=True) |
| 71 | +def pytest_terminal_summary(terminalreporter, exitstatus, config): |
| 72 | + total = terminalreporter._numcollected |
| 73 | + failed = len(terminalreporter.stats.get("failed", [])) |
| 74 | + passed = len(terminalreporter.stats.get("passed", [])) |
| 75 | + skipped = len(terminalreporter.stats.get("skipped", [])) |
| 76 | + |
| 77 | + # Rainbow banner |
| 78 | + print(big_banner("PYTEST PARTY REPORT")) |
| 79 | + |
| 80 | + if passed: |
| 81 | + emoji = random.choice(FLASHY_EMOJIS_PASS) |
| 82 | + print(rainbow_text(f"{emoji} {passed} TESTS PASSED {emoji}")) |
| 83 | + if failed: |
| 84 | + emoji = random.choice(FLASHY_EMOJIS_FAIL) |
| 85 | + print(rainbow_text(f"{emoji} {failed} TESTS FAILED {emoji}")) |
| 86 | + if skipped: |
| 87 | + emoji = random.choice(FLASHY_EMOJIS_SKIP) |
| 88 | + print(rainbow_text(f"{emoji} {skipped} TESTS SKIPPED {emoji}")) |
| 89 | + |
| 90 | + print(big_banner(f"TOTAL: {total}")) |
| 91 | + print(rainbow_text("🎆🎇✨ PYTEST PARTY OVER ✨🎇🎆")) |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +# # Only include colors supported by TerminalWriter.markup |
| 96 | +# FLASHY_COLORS = ["red", "green", "yellow", "blue", "cyan", "white"] |
| 97 | + |
| 98 | +# FLASHY_EMOJIS_PASS = ["🎉", "✨", "🌈", "💎", "🔥", "🚀", "💃"] |
| 99 | +# FLASHY_EMOJIS_FAIL = ["💀", "🔥", "💔", "😱", "👹", "🩸", "☠️"] |
| 100 | +# FLASHY_EMOJIS_SKIP = ["⏭", "🙃", "🕊", "💤", "📭"] |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +# def random_style(tw, text: str, emojis: list) -> str: |
| 106 | +# """Return text wrapped in random color and emoji bling.""" |
| 107 | +# color = random.choice(FLASHY_COLORS) |
| 108 | +# emoji = random.choice(emojis) |
| 109 | +# kwargs = {"bold": True, color: True} |
| 110 | +# return tw.markup(f"{emoji} {text} {emoji}", **kwargs) |
| 111 | + |
| 112 | + |
| 113 | +# def pytest_report_teststatus(report, config): |
| 114 | +# """Customize symbols in the progress line (dots → flashy).""" |
| 115 | +# if report.when == "call": |
| 116 | +# if report.failed: |
| 117 | +# return report.outcome, "💥", "FAILED FLASHY" |
| 118 | +# elif report.passed: |
| 119 | +# return report.outcome, "🌟", "PASSED FLASHY" |
| 120 | +# elif report.skipped: |
| 121 | +# return report.outcome, "⏭", "SKIPPED FLASHY" |
| 122 | + |
| 123 | + |
| 124 | +# @pytest.hookimpl(trylast=True) |
| 125 | +# def pytest_terminal_summary(terminalreporter, exitstatus, config): |
| 126 | +# tw = terminalreporter._tw |
| 127 | +# total = terminalreporter._numcollected |
| 128 | + |
| 129 | +# failed = len(terminalreporter.stats.get("failed", [])) |
| 130 | +# passed = len(terminalreporter.stats.get("passed", [])) |
| 131 | +# skipped = len(terminalreporter.stats.get("skipped", [])) |
| 132 | + |
| 133 | +# tw.sep("=", random_style(tw, "🌟🌟 TEST SUMMARY 🌟🌟", FLASHY_EMOJIS_PASS)) |
| 134 | + |
| 135 | +# if passed: |
| 136 | +# tw.write(random_style(tw, f"{passed} TESTS PASSED", FLASHY_EMOJIS_PASS) + "\n") |
| 137 | +# if failed: |
| 138 | +# tw.write(random_style(tw, f"{failed} TESTS FAILED", FLASHY_EMOJIS_FAIL) + "\n") |
| 139 | +# if skipped: |
| 140 | +# tw.write(random_style(tw, f"{skipped} TESTS SKIPPED", FLASHY_EMOJIS_SKIP) + "\n") |
| 141 | + |
| 142 | +# tw.sep("=", random_style(tw, f"TOTAL: {total}", FLASHY_EMOJIS_PASS)) |
| 143 | +# tw.write("\n") |
| 144 | +# tw.write(random_style(tw, "✨ PYTEST PARTY OVER ✨", FLASHY_EMOJIS_PASS) + "\n") |
0 commit comments