Hopefully an easy one. Trying to `.pants test` a ...
# general
e
Hopefully an easy one. Trying to
.pants test
a directory full of, well, tests. The
BUILD
has
sources=["***/**.py"
and includes all the test files. Except that there are a couple of files in there without actual tests; they're imported by the test files. Running the test target works, but those files print
=== no tests ran in 0.01s ===
and are listed by pants as "failed", ie
Copy code
❯ ./pants test --force some/file/without/tests.py
/home/vputz/.cache/pants/setup/bootstrap-Linux-x86_64/2.4.0.dev2+gitd46197d0_py38
15:29:46.22 [INFO] initializing scheduler...
15:29:46.55 [INFO] scheduler initialized.

𐄂 some/file/without/tests.py:../../tests failed.
within an appropriate venv,
Copy code
❯ pytest some/file/without/tests.py
======================================================== test session starts =========================================================
platform linux -- Python 3.9.0+, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /root/dir/wotever
plugins: some-plugins
collected 0 items                                                                                                                    

======================================================= no tests ran in 0.01s ========================================================
How can I avoid a "failed" with a "no tests ran" result--or skip the test running on files without tests (while still including them for dependencies)? If I
.pants test the_actual_test_target
it runs all the tests but lists the files without tests as "failed" (and thus the test target fails). If I
pytest
the directory representing the test target it doesn't even pick out the files without tests.
w
The 
BUILD
 has 
sources=["***/**.py"
  and includes all the test files.  Except that there are a couple of files in there without actual tests; they’re imported by the test files.
do you have separate targets for the tests and the libraries? i’m guessing not if you’re using that glob…
@echoing-farmer-15630: if you’re creating BUILD files from scratch for this repository, you might consider using
./pants tailor
? https://www.pantsbuild.org/docs/create-initial-build-files
in particular, it will create BUILD files per directory, and it will create separate test targets which default to owning the tests
e
By "libraries" if you mean the files with "test helper" functions, then no (the tests are dependent on a library from a different BUILD). I'll take a look at
./pants tailor
. I will say that I briefly tried putting the two "tools" files in the
dependencies
field and then changing the
sources
to only have the test sources, but then it complained that there wasn't a BUILD file down in there. Got to run for today likely, but I'll try
tailor
and see what it does.
w
@echoing-farmer-15630: ok, sounds good. yea, would recommend committing everything… and then effectively deleting all BUILD files before running
tailor
, and seeing what it does.
e
Actually, the rubber-ducking technique worked; creating a
python_library
target with
name='test_tools'
and then including
:test_tools
as a dependency in the
python_tests
target seems to have done the trick for now. Not quite how I anticipated that working to be honest, and it could still bite you I think if you marked all tests in a file as skipped, but I don't have time to test for now. I'll still check out
tailor
for fun tomorrow.
w
yea, would recommend it. when proparly laid out, you should need very little boilerplate in each BUILD file.
glad it’s working!
h
h
The
:test_tools
dependency should be inferrable, assuming you import from the test tools in the tests, so you could try leaving off that explict dependency (but keeping the
test_tools
target itself around).
Note that
tailor
will assume that anything named
tests.py
,
test_*.py
or
*_test.py
is a test, so if your tools file is literally named
test_tools.py
it will miscategorize it as a test. So you may want to rename or to manually override the targets generated by
tailor
in this case.