Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TST: Remove most */tests/__init__.py #29246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions doc/source/f2py/f2py-testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ The directory of the test suite looks like the following::
├── test_array_from_pyobj.py
├── // ... several test files
├── test_symbolic.py
└── util.py
testutils.py

Files starting with ``test_`` contain tests for various aspects of f2py from parsing
Fortran files to checking modules' documentation. ``src`` directory contains the
Fortran source files upon which we do the testing. ``util.py`` contains utility
functions for building and importing Fortran modules during test time using a
Fortran source files upon which we do the testing. ``testutils.py`` contains utility
functions for building and importing Fortran modules during test time using a
temporary location.

Adding a test
==============

F2PY's current test suite predates ``pytest`` and therefore does not use fixtures.
Instead, the test files contain test classes that inherit from ``F2PyTest``
class present in ``util.py``.
class present in ``testutils.py``.

.. literalinclude:: ../../../numpy/f2py/tests/util.py
.. literalinclude:: ../../../numpy/f2py/testutils.py
:language: python
:lines: 327-336
:linenos:

This class many helper functions for parsing and compiling test source files. Its child
This class many helper functions for parsing and compiling test source files. Its child
classes can override its ``sources`` data member to provide their own source files.
This superclass will then compile the added source files upon object creation and their
functions will be appended to ``self.module`` data member. Thus, the child classes will
Expand All @@ -70,13 +70,13 @@ Consider the following subroutines, contained in a file named :file:`add-test.f`
:language: fortran

The first routine `addb` simply takes an array and increases its elements by 1.
The second subroutine `addc` assigns a new array `k` with elements greater that
The second subroutine `addc` assigns a new array `k` with elements greater that
the elements of the input array `w` by 1.

A test can be implemented as follows::

class TestAdd(util.F2PyTest):
sources = [util.getpath("add-test.f")]
class TestAdd(testutils.F2PyTest):
sources = [testutils.getpath("add-test.f")]

def test_module(self):
k = np.array([1, 2, 3], dtype=np.float64)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions numpy/_core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,8 @@ python_sources = [
'_simd.pyi',
'_string_helpers.py',
'_string_helpers.pyi',
'_testutils_locales.py',
'_testutils_natype.py',
'_type_aliases.py',
'_type_aliases.pyi',
'_ufunc_config.py',
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/tests/test_longdouble.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

import numpy as np
from numpy._core.tests._locales import CommaDecimalPointLocale
from numpy._core._testutils_locales import CommaDecimalPointLocale
from numpy.testing import (
IS_MUSL,
assert_,
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import numpy as np
import numpy._core._multiarray_tests as _multiarray_tests
from numpy._core._rational_tests import rational
from numpy._core._testutils_locales import CommaDecimalPointLocale
from numpy._core.multiarray import _get_ndarray_c_version, dot
from numpy._core.tests._locales import CommaDecimalPointLocale
from numpy.exceptions import AxisError, ComplexWarning
from numpy.lib import stride_tricks
from numpy.lib.recfunctions import repack_fields
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/tests/test_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

import numpy as np
from numpy._core.tests._locales import CommaDecimalPointLocale
from numpy._core._testutils_locales import CommaDecimalPointLocale
from numpy.testing import IS_MUSL, assert_, assert_equal

_REF = {np.inf: 'inf', -np.inf: '-inf', np.nan: 'nan'}
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/tests/test_stringdtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import pytest

import numpy as np
from numpy._core.tests._natype import get_stringdtype_dtype as get_dtype
from numpy._core.tests._natype import pd_NA
from numpy._core._testutils_natype import get_stringdtype_dtype as get_dtype
from numpy._core._testutils_natype import pd_NA
from numpy.dtypes import StringDType
from numpy.testing import IS_PYPY, assert_array_equal

Expand Down
2 changes: 1 addition & 1 deletion numpy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import numpy
import numpy as np
from numpy._core._multiarray_tests import get_fpu_mode
from numpy._core.tests._natype import get_stringdtype_dtype, pd_NA
from numpy._core._testutils_natype import get_stringdtype_dtype, pd_NA
from numpy.testing._private.utils import NOGIL_BUILD

try:
Expand Down
File renamed without changes.
10 changes: 4 additions & 6 deletions numpy/f2py/tests/test_abstract_interface.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import pytest

from numpy.f2py import crackfortran
from numpy.f2py import crackfortran, testutils
from numpy.testing import IS_WASM

from . import util


@pytest.mark.skipif(IS_WASM, reason="Cannot start subprocess")
@pytest.mark.slow
class TestAbstractInterface(util.F2PyTest):
sources = [util.getpath("tests", "src", "abstract_interface", "foo.f90")]
class TestAbstractInterface(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "abstract_interface", "foo.f90")]

skip = ["add1", "add2"]

Expand All @@ -18,7 +16,7 @@ def test_abstract_interface(self):

def test_parse_abstract_interface(self):
# Test gh18403
fpath = util.getpath("tests", "src", "abstract_interface",
fpath = testutils.getpath("tests", "src", "abstract_interface",
"gh18403_mod.f90")
mod = crackfortran.crackfortran([str(fpath)])
assert len(mod) == 1
Expand Down
5 changes: 2 additions & 3 deletions numpy/f2py/tests/test_array_from_pyobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import numpy as np
from numpy._core._type_aliases import c_names_dict as _c_names_dict

from . import util
from numpy.f2py import testutils

wrap = None

Expand All @@ -34,7 +33,7 @@ def setup_module():
src = [
get_testdir() / "wrapmodule.c",
]
wrap = util.build_meson(src, module_name="test_array_from_pyobj_ext")
wrap = testutils.build_meson(src, module_name="test_array_from_pyobj_ext")


def flags_info(arr):
Expand Down
14 changes: 7 additions & 7 deletions numpy/f2py/tests/test_assumed_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

import pytest

from . import util
from numpy.f2py import testutils


class TestAssumedShapeSumExample(util.F2PyTest):
class TestAssumedShapeSumExample(testutils.F2PyTest):
sources = [
util.getpath("tests", "src", "assumed_shape", "foo_free.f90"),
util.getpath("tests", "src", "assumed_shape", "foo_use.f90"),
util.getpath("tests", "src", "assumed_shape", "precision.f90"),
util.getpath("tests", "src", "assumed_shape", "foo_mod.f90"),
util.getpath("tests", "src", "assumed_shape", ".f2py_f2cmap"),
testutils.getpath("tests", "src", "assumed_shape", "foo_free.f90"),
testutils.getpath("tests", "src", "assumed_shape", "foo_use.f90"),
testutils.getpath("tests", "src", "assumed_shape", "precision.f90"),
testutils.getpath("tests", "src", "assumed_shape", "foo_mod.f90"),
testutils.getpath("tests", "src", "assumed_shape", ".f2py_f2cmap"),
]

@pytest.mark.slow
Expand Down
7 changes: 3 additions & 4 deletions numpy/f2py/tests/test_block_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import pytest

from numpy.f2py import testutils
from numpy.testing import IS_PYPY

from . import util


@pytest.mark.slow
class TestBlockDocString(util.F2PyTest):
sources = [util.getpath("tests", "src", "block_docstring", "foo.f")]
class TestBlockDocString(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "block_docstring", "foo.f")]

@pytest.mark.skipif(sys.platform == "win32",
reason="Fails with MinGW64 Gfortran (Issue #9673)")
Expand Down
25 changes: 12 additions & 13 deletions numpy/f2py/tests/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import pytest

import numpy as np
from numpy.f2py import testutils
from numpy.testing import IS_PYPY

from . import util


class TestF77Callback(util.F2PyTest):
sources = [util.getpath("tests", "src", "callback", "foo.f")]
class TestF77Callback(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "callback", "foo.f")]

@pytest.mark.parametrize("name", ["t", "t2"])
@pytest.mark.slow
Expand Down Expand Up @@ -206,8 +205,8 @@ class TestF77CallbackPythonTLS(TestF77Callback):
options = ["-DF2PY_USE_PYTHON_TLS"]


class TestF90Callback(util.F2PyTest):
sources = [util.getpath("tests", "src", "callback", "gh17797.f90")]
class TestF90Callback(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "callback", "gh17797.f90")]

@pytest.mark.slow
def test_gh17797(self):
Expand All @@ -219,13 +218,13 @@ def incr(x):
assert r == 123 + 1 + 2 + 3


class TestGH18335(util.F2PyTest):
class TestGH18335(testutils.F2PyTest):
"""The reproduction of the reported issue requires specific input that
extensions may break the issue conditions, so the reproducer is
implemented as a separate test class. Do not extend this test with
other tests!
"""
sources = [util.getpath("tests", "src", "callback", "gh18335.f90")]
sources = [testutils.getpath("tests", "src", "callback", "gh18335.f90")]

@pytest.mark.slow
def test_gh18335(self):
Expand All @@ -236,9 +235,9 @@ def foo(x):
assert r == 123 + 1


class TestGH25211(util.F2PyTest):
sources = [util.getpath("tests", "src", "callback", "gh25211.f"),
util.getpath("tests", "src", "callback", "gh25211.pyf")]
class TestGH25211(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "callback", "gh25211.f"),
testutils.getpath("tests", "src", "callback", "gh25211.pyf")]
module_name = "callback2"

def test_gh25211(self):
Expand All @@ -253,8 +252,8 @@ def bar(x):
@pytest.mark.xfail(condition=(platform.system().lower() == 'darwin'),
run=False,
reason="Callback aborts cause CI failures on macOS")
class TestCBFortranCallstatement(util.F2PyTest):
sources = [util.getpath("tests", "src", "callback", "gh26681.f90")]
class TestCBFortranCallstatement(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "callback", "gh26681.f90")]
options = ['--lower']

def test_callstatement_fortran(self):
Expand Down
32 changes: 16 additions & 16 deletions numpy/f2py/tests/test_character.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import pytest

import numpy as np
from numpy.f2py.tests import util
from numpy.f2py import testutils
from numpy.testing import assert_array_equal, assert_equal, assert_raises


@pytest.mark.slow
class TestCharacterString(util.F2PyTest):
class TestCharacterString(testutils.F2PyTest):
# options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
suffix = '.f90'
fprefix = 'test_character_string'
Expand Down Expand Up @@ -134,7 +134,7 @@ def test_2d_array_input(self, length):
assert_array_equal(f(a), expected)


class TestCharacter(util.F2PyTest):
class TestCharacter(testutils.F2PyTest):
# options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
suffix = '.f90'
fprefix = 'test_character'
Expand Down Expand Up @@ -432,7 +432,7 @@ def test_optional(self):
assert_equal(f(b'B'), b"B")


class TestMiscCharacter(util.F2PyTest):
class TestMiscCharacter(testutils.F2PyTest):
# options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
suffix = '.f90'
fprefix = 'test_misc_character'
Expand Down Expand Up @@ -575,8 +575,8 @@ def test_character_bc(self, state):
assert_raises(Exception, lambda: f(b'c'))


class TestStringScalarArr(util.F2PyTest):
sources = [util.getpath("tests", "src", "string", "scalar_string.f90")]
class TestStringScalarArr(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "string", "scalar_string.f90")]

def test_char(self):
for out in (self.module.string_test.string,
Expand All @@ -594,15 +594,15 @@ def test_char_arr(self):
expected = '|S12'
assert out.dtype == expected

class TestStringAssumedLength(util.F2PyTest):
sources = [util.getpath("tests", "src", "string", "gh24008.f")]
class TestStringAssumedLength(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "string", "gh24008.f")]

def test_gh24008(self):
self.module.greet("joe", "bob")

@pytest.mark.slow
class TestStringOptionalInOut(util.F2PyTest):
sources = [util.getpath("tests", "src", "string", "gh24662.f90")]
class TestStringOptionalInOut(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "string", "gh24662.f90")]

def test_gh24662(self):
self.module.string_inout_optional()
Expand All @@ -615,11 +615,11 @@ def test_gh24662(self):


@pytest.mark.slow
class TestNewCharHandling(util.F2PyTest):
class TestNewCharHandling(testutils.F2PyTest):
# from v1.24 onwards, gh-19388
sources = [
util.getpath("tests", "src", "string", "gh25286.pyf"),
util.getpath("tests", "src", "string", "gh25286.f90")
testutils.getpath("tests", "src", "string", "gh25286.pyf"),
testutils.getpath("tests", "src", "string", "gh25286.f90")
]
module_name = "_char_handling_test"

Expand All @@ -628,11 +628,11 @@ def test_gh25286(self):
assert info == 2

@pytest.mark.slow
class TestBCCharHandling(util.F2PyTest):
class TestBCCharHandling(testutils.F2PyTest):
# SciPy style, "incorrect" bindings with a hook
sources = [
util.getpath("tests", "src", "string", "gh25286_bc.pyf"),
util.getpath("tests", "src", "string", "gh25286.f90")
testutils.getpath("tests", "src", "string", "gh25286_bc.pyf"),
testutils.getpath("tests", "src", "string", "gh25286.f90")
]
module_name = "_char_handling_test"

Expand Down
11 changes: 5 additions & 6 deletions numpy/f2py/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pytest

import numpy as np

from . import util
from numpy.f2py import testutils


@pytest.mark.slow
class TestCommonBlock(util.F2PyTest):
sources = [util.getpath("tests", "src", "common", "block.f")]
class TestCommonBlock(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "common", "block.f")]

def test_common_block(self):
self.module.initcb()
Expand All @@ -16,8 +15,8 @@ def test_common_block(self):
assert self.module.block.ok == np.array(3, dtype=np.int32)


class TestCommonWithUse(util.F2PyTest):
sources = [util.getpath("tests", "src", "common", "gh19161.f90")]
class TestCommonWithUse(testutils.F2PyTest):
sources = [testutils.getpath("tests", "src", "common", "gh19161.f90")]

def test_common_gh19161(self):
assert self.module.data.x == 0
Loading
Loading