Skip to content

Commit

Permalink
feat: hint to run doctest with a per function context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri-ColibrITD committed May 14, 2024
1 parent dc62670 commit 008c3ee
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions tests/doctest_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import doctest
import os

import numpy as np

import mpqp
from mpqp.all import *
from mpqp.execution import BatchResult
from mpqp.execution import remote_result_from_id
from mpqp.core.instruction.measurement import pauli_string
from mpqp.core.instruction.measurement.pauli_string import PauliString
from mpqp.tools.maths import is_unitary, is_hermitian, normalize
from mpqp.tools.generics import find, flatten
from mpqp.execution import BatchResult, remote_result_from_id
from mpqp.qasm import open_qasm_2_to_3
from mpqp.tools.generics import find, flatten
from mpqp.tools.maths import is_hermitian, is_unitary, normalize

test_globals = globals().copy()
test_globals.update(locals())


def run_doctests_in_folder(folder_path):
"""
Run doctests on all Python files in the specified folder and its subfolders.
Expand All @@ -22,7 +25,7 @@ def run_doctests_in_folder(folder_path):
for root, _, files in os.walk(folder_path):
for filename in files:
if filename.endswith(".py"):
file_path = os.path.join("..\\"+root, filename)
file_path = os.path.join("..\\" + root, filename)
print(f"Running doctests in {os.path.join(os.getcwd(),root,filename)}")
doctest.testfile(file_path, globs=test_globals)

Expand All @@ -32,3 +35,22 @@ def run_doctests_in_folder(folder_path):

# Run doctests on all Python files in the main folder and its subfolders
run_doctests_in_folder(main_folder_path)

if False:
from doctest import DocTestFinder, DocTestRunner

finder = DocTestFinder()
runner = DocTestRunner()

class SafeRunner:
def __enter__(self):
# move mpqp to mpqp.bak
pass

def __exit__(self, exc_type, exc_value, exc_tb):
# move mpqp.bak to mpqp
pass

for test in finder.find(mpqp, "mpqp", globs=test_globals):
with SafeRunner():
runner.run(test)

0 comments on commit 008c3ee

Please sign in to comment.