-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: move reset seed to fixture [1/2] (#2702)
* move reset seed to fixture * updating test outputs * Update src/conftest.py
- Loading branch information
Showing
51 changed files
with
284 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from lightning_utilities.core.imports import package_available | ||
|
||
if package_available("pytest") and package_available("doctest"): | ||
import doctest | ||
|
||
import pytest | ||
|
||
MANUAL_SEED = doctest.register_optionflag("MANUAL_SEED") | ||
|
||
@pytest.fixture(autouse=True) | ||
def reset_random_seed(seed: int = 42) -> None: # noqa: PT004 | ||
"""Reset the random seed before running each doctest.""" | ||
import random | ||
|
||
import numpy as np | ||
import torch | ||
|
||
random.seed(seed) | ||
np.random.seed(seed) | ||
torch.manual_seed(seed) | ||
torch.cuda.manual_seed_all(seed) | ||
|
||
class DoctestModule(pytest.Module): | ||
"""A custom module class that augments collected doctests with the reset_random_seed fixture.""" | ||
|
||
def collect(self) -> GeneratorExit: | ||
"""Augment collected doctests with the reset_random_seed fixture.""" | ||
for item in super().collect(): | ||
if isinstance(item, pytest.DoctestItem): | ||
item.add_marker(pytest.mark.usefixtures("reset_random_seed")) | ||
yield item | ||
|
||
def pytest_collect_file(parent: Path, path: Path) -> Optional[DoctestModule]: | ||
"""Collect doctests and add the reset_random_seed fixture.""" | ||
if path.ext == ".py": | ||
return DoctestModule.from_parent(parent, fspath=path) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.