rich-kite-32423
12/27/2022, 9:42 PMpytest.ini
in the root directory contains:
[pytest]
minversion = 7.0.1
python_files = *.py
addopts = -rA
The python_files = *.py
should make pytest look in all files for tests, according to https://docs.pytest.org/en/7.1.x/example/pythoncollection.html#customizing-test-collection, but it doesn't seem to be; it only seems to look in `foo_test.py`:
bruce@groot:~/RethinkingObjects$ ./pants test ::
14:40:42.23 [ERROR] Completed: Run Pytest - test_experiments/foo_test.py:tests failed (exit code 5).
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.0.1, pluggy-1.0.0
rootdir: /tmp/pants-sandbox-pDDAT5, configfile: pytest.ini
plugins: forked-1.4.0, cov-3.0.0, xdist-2.5.0
collected 0 items
- generated xml file: /tmp/pants-sandbox-pDDAT5/test_experiments.foo_test.py.tests.xml -
============================ no tests ran in 0.02s =============================
enough-analyst-54434
12/27/2022, 9:46 PMpython_files
are ~never going to work. Looking ...enough-analyst-54434
12/27/2022, 9:51 PM$ git diff
diff --git a/test_experiments/foo_test.py b/test_experiments/foo_test.py
index 8255e7f..ef0e3aa 100644
--- a/test_experiments/foo_test.py
+++ b/test_experiments/foo_test.py
@@ -1,3 +1,3 @@
-def foo2_test():
+def test_foo2():
print("running foo_test_2")
assert False
And:
$ pants test ::
14:51:21.24 [ERROR] Completed: Run Pytest - test_experiments/foo_test.py:tests failed (exit code 1).
============================= test session starts ==============================
platform linux -- Python 3.10.7, pytest-7.0.1, pluggy-1.0.0
rootdir: /tmp/pants-sandbox-MCekm2, configfile: pytest.ini
plugins: forked-1.4.0, cov-3.0.0, xdist-2.5.0
collected 1 item
test_experiments/foo_test.py F [100%]
=================================== FAILURES ===================================
__________________________________ test_foo2 ___________________________________
def test_foo2():
print("running foo_test_2")
> assert False
E assert False
test_experiments/foo_test.py:3: AssertionError
----------------------------- Captured stdout call -----------------------------
running foo_test_2
- generated xml file: /tmp/pants-sandbox-MCekm2/test_experiments.foo_test.py.tests.xml -
=========================== short test summary info ============================
FAILED test_experiments/foo_test.py::test_foo2 - assert False
============================== 1 failed in 0.03s ===============================
ā test_experiments/foo_test.py:tests failed in 0.20s.
enough-analyst-54434
12/27/2022, 9:53 PM$ pex pytest -cpytest -- test_experiments/foo_test.py
============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.10.6, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/jsirois/dev/BruceEckel/RethinkingObjects, configfile: pytest.ini
collected 0 items
============================================================================================= no tests ran in 0.00s ==============================================================================================
rich-kite-32423
12/27/2022, 10:50 PMpytest.ini
file. Pytest does not appear to be recursing the directories. To test this I put a couple of test files at the top level:enough-analyst-54434
12/27/2022, 10:51 PMrich-kite-32423
12/27/2022, 10:53 PMf1_test.py
and test_f1.py
. It finds these but only registers test functions if they start with the word "test", not end with "test" as the Pytest documentation seems to say. The output is:
bruce@groot:~/RethinkingObjects$ pytest
================================================= test session starts ==================================================
platform linux -- Python 3.10.6, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/bruce/RethinkingObjects
collected 3 items
f1_test.py F [ 33%]
test_f1.py F [ 66%]
test_f2.py . [100%]
======================================================= FAILURES =======================================================
______________________________________________________ test_f1_b _______________________________________________________
def test_f1_b():
> assert False
E assert False
f1_test.py:5: AssertionError
_______________________________________________________ test_f1 ________________________________________________________
def test_f1():
> assert False
E assert False
test_f1.py:5: AssertionError
=============================================== short test summary info ================================================
FAILED f1_test.py::test_f1_b - assert False
FAILED test_f1.py::test_f1 - assert False
============================================= 2 failed, 1 passed in 0.02s ==============================================
These have been added to https://github.com/BruceEckel/RethinkingObjectsenough-analyst-54434
12/27/2022, 10:53 PMrich-kite-32423
12/27/2022, 10:55 PMenough-analyst-54434
12/27/2022, 10:56 PMrich-kite-32423
12/27/2022, 10:57 PMrich-kite-32423
12/27/2022, 11:00 PMenough-analyst-54434
12/27/2022, 11:01 PMrich-kite-32423
12/27/2022, 11:02 PMrich-kite-32423
12/27/2022, 11:08 PMenough-analyst-54434
12/27/2022, 11:11 PMrich-kite-32423
12/27/2022, 11:45 PM./pants test ::
doesn't produce any results.rich-kite-32423
12/27/2022, 11:45 PMenough-analyst-54434
12/27/2022, 11:59 PMpython_tests
target has its own naming conventions; you're violating them. You want to carefully spell out sources=[...]
for python_tests
targets and remove certain python_sources
targets that would otherwise catch things like a.py
.
You are bucking conventions and being forced to learn what the defaults are in essence. If you're going to do that you have to expect to do a lot of reading and explicit configuration, or get lucky and have someone chime in and save you effort like I've done hereenough-analyst-54434
12/28/2022, 12:01 AMhappy-kitchen-89482
12/28/2022, 4:57 PMhappy-kitchen-89482
12/28/2022, 4:58 PMhappy-kitchen-89482
12/28/2022, 4:58 PMhappy-kitchen-89482
12/28/2022, 4:58 PMhappy-kitchen-89482
12/28/2022, 4:59 PMhappy-kitchen-89482
12/28/2022, 5:00 PMrich-kite-32423
12/28/2022, 6:36 PM_main
. The pytest.ini
file which does this is:
[pytest]
minversion = 7.0.1
python_files = *.py # Discover tests in every python file
python_functions = *_main
addopts = -rA
This works correctly with Pytest and came from following the Pytest docs. (In those docs they just seem to consider it normal configuration options, and not "bucking conventions," so I was a bit surprised at that reference).
I'm confused, though --- are you saying I shouldn't/cannot use pytest.ini
at all, and must do all the configuration inside the BUILD
file? And it sounds like that configuration looks like this:
python_tests(sources = ["*.py"])
Also, I'm not sure what "subtracting" looks like, or how to know which files to subtract (especially as the goal is to be able to put tests in any .py
file).rich-kite-32423
12/28/2022, 6:37 PMenough-analyst-54434
12/28/2022, 6:54 PMenough-analyst-54434
12/28/2022, 6:55 PMenough-analyst-54434
12/28/2022, 6:57 PMenough-analyst-54434
12/28/2022, 6:58 PMenough-analyst-54434
12/28/2022, 7:00 PMrich-kite-32423
12/28/2022, 7:01 PMpytest.ini
file shown above. I am indeed doing something different here, and will explain it in the book (which is not a beginner book).enough-analyst-54434
12/28/2022, 7:01 PMhappy-kitchen-89482
12/28/2022, 7:13 PMpython_tests(sources = ["*.py"])
is correct and subtraction from python_sources is moot, since you'd be subtracting everything, so you would just omit the python_sources()
entirely.happy-kitchen-89482
12/28/2022, 7:13 PMpython_tests()
, if your examples have complex dependencies across different directories?happy-kitchen-89482
12/28/2022, 7:13 PMhappy-kitchen-89482
12/28/2022, 7:14 PMhappy-kitchen-89482
12/28/2022, 7:14 PMhappy-kitchen-89482
12/28/2022, 7:15 PMhappy-kitchen-89482
12/28/2022, 7:15 PMenough-analyst-54434
12/28/2022, 10:20 PMenough-analyst-54434
12/28/2022, 10:21 PMenough-analyst-54434
12/28/2022, 10:25 PMpex_binary
target there on a "lib" python_sources
target that I wouldn't normally have had to. That's almost certainly due to the dual-target ownership. I think you'll have hell to pay with dep inference not generally working in this sort of setup because of the dual ownership.enough-analyst-54434
12/28/2022, 10:26 PMhappy-kitchen-89482
12/28/2022, 10:30 PMrich-kite-32423
12/29/2022, 4:24 PM