Skip to content
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

Test suite cleanup and reorganisation #898

Merged
merged 5 commits into from
Jun 27, 2017

Conversation

dean0x7d
Copy link
Member

@dean0x7d dean0x7d commented Jun 10, 2017

The first part of this PR closes #861: the test_issues.cpp/.py files are removed completely and the tests are redistributed into more appropriate files.

Part two breaks up test_python_types.cpp because it has also started to accumulate various not-so-related parts. The tests are redistributed into: test_builtin_casters for casters which are available in pybind11 without extra headers, test_stl for stl.h and test_pytypes for Python type wrappers and their C++ interfaces. There were also a couple of py::class_ tests which were moved into test_class (renamed from test_class_args).

Along the way, this also adds a few things to the main pybind11_tests.h header:

  • UnregisteredType which is used to test conversion errors. A few test files were already using various versions of this.
  • UserType which serves as the official user-defined type for testing (simple int holder). Multiple tests have their own versions of this (or use some locally exported class) but a lot of that is unnecessary duplication (I haven't gone through to replace everything yet).
  • IncType: adapted from @jagerman's reference_wrapper tests. It's like UserType, but increments value on copy for quick reference vs. copy tests. Should be useful for quick tests without the need for full ConstructorStats instrumentation.
  • TEST_SUBMODULE is a convenience macro which wraps test_initializer and defines a new submodule (instead of adding to the main pybind11_tests module). It also defines a function instead of a lambda because MSVC has issues with lambdas and local structs.

Proposal for new test conventions:

  • Each .cpp file should create its own submodule. (Made easier by TEST_SUBMODULE).
  • Each .py files imports that module as from pybind11_tests import submodule as m. This should reduce the number of import statements and the m name reflects the convention from the .cpp files.
  • If a .py files has a function called test_name() then the .cpp file should add a corresponding // test_name comment above the bindings. This should help navigation between files.
  • Where possible, it would be preferable to define local classes (at function scope) right above their py::class_ bindings and related lambda def bindings. As long as the classes are single-purpose for a specific test, this should help with organization. MSVC would frequently choke on this which made it impractical, but the TEST_SUBMODULE macro resolves that.

Thoughts?

Right now this is only applied to the new test files which have come out of test_python_types. If this seems good, it could be applied everywhere. There are also some other frequently reinvented classes which can be pulled out (seems like a move-only type is needed frequently for testing, etc.).

Closes #842, closes #861.

@jagerman
Copy link
Member

This all sounds pretty good to me. Breaking up of test_python_types was definitely the next logical step after dispersing test_issues.

I'm more lukewarm on coalescing common types. I worry a bit that this is going to run into conflicts if pushed too far because future tests are going to want to change something something which in turn breaks other tests.

As for the m. everywhere, I have to say I prefer the from m import x, y, z rather than having dozens of m.x(), m.y(), etc., particular when functions are often repeated (for example, in test_single_char_arguments()). I think this should be on more of a per-test basis: sometimes importing is going to be nicer, sometimes just using m.whatever() will be nicer (in much the same way that a localized using namespace detail; can make some code cleaner).

As for moving into submodules, I think that's a great idea: I used to run into cases where cmake -DPYBIND11_TEST_OVERRIDE=whatever didn't work properly because a test relied on something set in one of the other tests. I think most of those are gone now, but isolating things to separate modules will make sure of that.

@dean0x7d
Copy link
Member Author

I'm more lukewarm on coalescing common types. I worry a bit that this is going to run into conflicts if pushed too far because future tests are going to want to change something which in turn breaks other tests.

The intention here isn't to consolidate everything -- only the most common types in order to remove needless duplication. If a test needs something very specific then it's fine to introduce a new class only in its translation unit. But if a test just needs to check builtin vs. user-defined types then it doesn't make sense to create yet another int-holder.

As for the m. everywhere, I have to say I prefer the from m import x, y, z rather than having dozens of m.x(), m.y(), etc., particular when functions are often repeated (for example, in test_single_char_arguments()). I think this should be on more of a per-test basis: sometimes importing is going to be nicer, sometimes just using m.whatever() will be nicer (in much the same way that a localized using namespace detail; can make some code cleaner).

Sure, that makes sense. If a test uses a function very frequently, from m import x is definitely nicer. The m. convention shouldn't be a strict rule, just a good default because the majority of the test functions are used exactly once, so this saves on very long from m import (a, b, ..., y, z, ...) statements.

@jagerman
Copy link
Member

I suggest we merge this one now (after also moving the new, conflicting tests added to test_python_types), and deal with transitioning the others one by one. Otherwise I think it's going to be painful keeping up with the resulting merge conflicts.

@dean0x7d dean0x7d changed the title WIP: Test suite cleanup and reorganisation Test suite cleanup and reorganisation Jun 25, 2017
`nullptr` is not expected to work in this case.
@dean0x7d
Copy link
Member Author

I suggest we merge this one now (after also moving the new, conflicting tests added to test_python_types), and deal with transitioning the others one by one. Otherwise I think it's going to be painful keeping up with the resulting merge conflicts.

Agreed. I've rebased and resolved the conflicts with the new tests.

I've also included a few smaller changes:

  • Consolidated a few shorter test files:

    Moved test_alias_initialization into test_virtual_functions -- test alias_init tests are very short and logically part of virtual_functions.

    Moved test_inheritance into test_class -- again, short tests, but they were actually checking more than just inheritance. Besides, single-inheritance is simple enough to be considered basic functionality of class_. The multiple inheritance tests are much more involved and therefore have a separate file.

  • Moved the CMake code related to test_cmake_build into its own subdirectory -- makes the main pytest CMake code cleaner.

  • Since test_issues.cpp is going away, I've cherry picked the new test from Add tests for pointer defaults. #842.

If there are no objections I'd like to merge this soon to avoid more conflicts with PRs. Porting the other tests to submodules can be done gradually later.

Note: I have accidentally pushed the latest changes to both my fork and origin. I've deleted the branch on origin, but Travis and AppVeyor have already registered it and now show both the PR tests and branch tests. The latter will fail because the branch has been deleted.

@dean0x7d dean0x7d merged commit 34b7b54 into pybind:master Jun 27, 2017
@dean0x7d dean0x7d deleted the test-suite-cleanup branch June 29, 2017 09:46
jagerman added a commit to jagerman/pybind11 that referenced this pull request Aug 4, 2017
This udpates all the remaining tests to the new test suite code and
comment styles started in pybind#898.  For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.

- test_constants_and_functions: this adds more overload tests with
  overloads with different number of arguments for more comprehensive
  overload_cast testing.  The test style conversion broke the overload
  tests under MSVC 2015, prompting the additional tests while looking
  for a workaround.

- test_eigen: this dropped some unused definitions `get_cm_corners` and
  `get_cm_corners_const`--these same tests were duplicates of the same
  things provided (and used) via ReturnTester methods.

- test_opaque_types: this test had a hidden dependence on ExampleMandA
  which is now fixed by using the global UserType which suffices for the
  relevant test.

- test_methods_and_attributes: this required some additions to UserType
  to make it usable as a replacement for the test's previous SimpleType:
  UserType gained a value mutator, and the `value` property is not
  mutable (it was previously readonly).  Some overload tests were also
  added to better test overload_cast (as described above).

- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
  the templated versions with an empty parameter pack expand to the same
  thing.

- test_stl: this was already mostly in the new style; this just tweaks
  things a bit, localizing a class, and adding some missing
  `// test_whatever` comments.

- test_virtual_functions: like `test_stl`, this was mostly in the new
  test style already, but needed some `// test_whatever` comments.
  This commit also moves the inherited virtual example code to the end
  of the file, after the main set of tests (since it is less important
  than the other tests, and rather length); it also got renamed to
  `test_inherited_virtuals` (from `test_inheriting_repeat`) because it
  tests both inherited virtual approaches, not just the repeat approach.
jagerman added a commit to jagerman/pybind11 that referenced this pull request Aug 5, 2017
This udpates all the remaining tests to the new test suite code and
comment styles started in pybind#898.  For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.

- test_constants_and_functions: this adds more overload tests with
  overloads with different number of arguments for more comprehensive
  overload_cast testing.  The test style conversion broke the overload
  tests under MSVC 2015, prompting the additional tests while looking
  for a workaround.

- test_eigen: this dropped the unused functions `get_cm_corners` and
  `get_cm_corners_const`--these same tests were duplicates of the same
  things provided (and used) via ReturnTester methods.

- test_opaque_types: this test had a hidden dependence on ExampleMandA
  which is now fixed by using the global UserType which suffices for the
  relevant test.

- test_methods_and_attributes: this required some additions to UserType
  to make it usable as a replacement for the test's previous SimpleType:
  UserType gained a value mutator, and the `value` property is not
  mutable (it was previously readonly).  Some overload tests were also
  added to better test overload_cast (as described above).

- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
  the templated versions with an empty parameter pack expand to the same
  thing.

- test_stl: this was already mostly in the new style; this just tweaks
  things a bit, localizing a class, and adding some missing
  `// test_whatever` comments.

- test_virtual_functions: like `test_stl`, this was mostly in the new
  test style already, but needed some `// test_whatever` comments.
  This commit also moves the inherited virtual example code to the end
  of the file, after the main set of tests (since it is less important
  than the other tests, and rather length); it also got renamed to
  `test_inherited_virtuals` (from `test_inheriting_repeat`) because it
  tests both inherited virtual approaches, not just the repeat approach.
jagerman added a commit that referenced this pull request Aug 5, 2017
This udpates all the remaining tests to the new test suite code and
comment styles started in #898.  For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.

- test_constants_and_functions: this adds more overload tests with
  overloads with different number of arguments for more comprehensive
  overload_cast testing.  The test style conversion broke the overload
  tests under MSVC 2015, prompting the additional tests while looking
  for a workaround.

- test_eigen: this dropped the unused functions `get_cm_corners` and
  `get_cm_corners_const`--these same tests were duplicates of the same
  things provided (and used) via ReturnTester methods.

- test_opaque_types: this test had a hidden dependence on ExampleMandA
  which is now fixed by using the global UserType which suffices for the
  relevant test.

- test_methods_and_attributes: this required some additions to UserType
  to make it usable as a replacement for the test's previous SimpleType:
  UserType gained a value mutator, and the `value` property is not
  mutable (it was previously readonly).  Some overload tests were also
  added to better test overload_cast (as described above).

- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
  the templated versions with an empty parameter pack expand to the same
  thing.

- test_stl: this was already mostly in the new style; this just tweaks
  things a bit, localizing a class, and adding some missing
  `// test_whatever` comments.

- test_virtual_functions: like `test_stl`, this was mostly in the new
  test style already, but needed some `// test_whatever` comments.
  This commit also moves the inherited virtual example code to the end
  of the file, after the main set of tests (since it is less important
  than the other tests, and rather length); it also got renamed to
  `test_inherited_virtuals` (from `test_inheriting_repeat`) because it
  tests both inherited virtual approaches, not just the repeat approach.
@dean0x7d dean0x7d modified the milestone: v2.2 Aug 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants