how to debug pants tests?

1,381 views
Skip to first unread message

Jin Feng

unread,
Aug 20, 2014, 10:54:55 AM8/20/14
to pants...@googlegroups.com, Jin Feng
Hi folks,


------------

...where you first want to break. If the code is in a test, instead use


import pytest; pytest.set_trace()

To run tests and bring up pdb for failing tests, you can instead pass --pdb:


$ ./pants tests/python/pants_test/tasks: --pdb
------------

But when I run, this pants comment, it says --pdb no such an option. How do you guys do this, other than putting tons of print()'s.

Thanks
   Jin Feng

John Sirois

unread,
Aug 20, 2014, 11:23:29 AM8/20/14
to Jin Feng, pants-devel
1st - if you're using the IDEA setup [1] you can debug inside the ide just like you would junit tests - right-click, run-debug.
2nd - Use this CLI: ./pants build tests/python/pants_test/tasks: --pdb
3rd - Do it the modern way and realize the old way (2 above) just passes extra flags through to pytest:
$ PANTS_DEV=1 ./pants goal help test
...
test:pytest options:
  --test-pytest-timeout=PYTHON_CONN_TIMEOUT
                          Number of seconds to wait for http connections.
  --test-pytest-fast, --no-test-pytest-fast
                          [True] Run all tests in a single chroot. If set to
                          false, each test target will create a new chroot,
                          which will be much slower.
  --test-pytest-options=PYTEST_RUN_OPTIONS
                          [[]] options to pass to the underlying pytest runner.
...

So: $ PANTS_DEV=1 ./pants goal test tests/python/pants_test/tasks: --test-pytest-options=--pdb

--
John Sirois
303-512-3301





Jin Feng

unread,
Aug 20, 2014, 12:05:40 PM8/20/14
to John Sirois, pants-devel
Thanks! I've gone [1]. Now when I right click a test_no_targets method, and select Debug 'test_build_graph' in the menu, it fails with:

/Users/jinfeng/workspace/github-pants/build-support/pants_dev_deps.venv/bin/python "/Users/jinfeng/Library/Application Support/IntelliJIdea13/python/helpers/pydev/pydevd.py" --multiproc --client 127.0.0.1 --port 61127 --file /Users/jinfeng/workspace/github-pants/tests/python/pants_test/graph/test_build_graph.py
pydev debugger: process 25831 is connecting

Connected to pydev debugger (build 135.1230)
Traceback (most recent call last):
  File "/Users/jinfeng/Library/Application Support/IntelliJIdea13/python/helpers/pydev/pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "/Users/jinfeng/Library/Application Support/IntelliJIdea13/python/helpers/pydev/pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/jinfeng/workspace/github-pants/tests/python/pants_test/graph/test_build_graph.py", line 20, in <module>
    from pants_test.base_test import BaseTest
  File "/Users/jinfeng/workspace/github-pants/tests/python/pants_test/base_test.py", line 12, in <module>
    import unittest2
ImportError: No module named unittest2

Process finished with exit code 1

Any PYTHONHOME/PYTHONPATH setting I did incorrectly in the IntelliJ Setup?



Thanks
   Jin Feng

John Sirois

unread,
Aug 20, 2014, 12:09:27 PM8/20/14
to Jin Feng, pants-devel
On Wed, Aug 20, 2014 at 10:05 AM, Jin Feng <jin...@twitter.com> wrote:
Thanks! I've gone [1]. Now when I right click a test_no_targets method, and select Debug 'test_build_graph' in the menu, it fails with:

/Users/jinfeng/workspace/github-pants/build-support/pants_dev_deps.venv/bin/python "/Users/jinfeng/Library/Application Support/IntelliJIdea13/python/helpers/pydev/pydevd.py" --multiproc --client 127.0.0.1 --port 61127 --file /Users/jinfeng/workspace/github-pants/tests/python/pants_test/graph/test_build_graph.py
pydev debugger: process 25831 is connecting

Connected to pydev debugger (build 135.1230)
Traceback (most recent call last):
  File "/Users/jinfeng/Library/Application Support/IntelliJIdea13/python/helpers/pydev/pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "/Users/jinfeng/Library/Application Support/IntelliJIdea13/python/helpers/pydev/pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/jinfeng/workspace/github-pants/tests/python/pants_test/graph/test_build_graph.py", line 20, in <module>
    from pants_test.base_test import BaseTest
  File "/Users/jinfeng/workspace/github-pants/tests/python/pants_test/base_test.py", line 12, in <module>
    import unittest2
ImportError: No module named unittest2

Process finished with exit code 1

Any PYTHONHOME/PYTHONPATH setting I did incorrectly in the IntelliJ Setup?

This should be fixed in the PANTS_DEV=1 bootstrapping / [1] docs, but my hack is to write:
import unittest2

Then choose the red-squiggly option that fetches the thing, then delete the import.



--
John Sirois
303-512-3301





Jin Feng

unread,
Aug 20, 2014, 12:15:54 PM8/20/14
to John Sirois, pants-devel
What option? Didn't see any download/fetch/get...

Inline image 1


Thanks
   Jin Feng

John Sirois

unread,
Aug 20, 2014, 12:31:12 PM8/20/14
to Jin Feng, pants-devel
I'm on linux - so its alt-enter with the caret on the red-squiggle:



--
John Sirois
303-512-3301





Jin Feng

unread,
Aug 20, 2014, 12:39:42 PM8/20/14
to John Sirois, pants-devel
Ha, worked. Same on mac os x, Option + Enter. Thanks!


Thanks
   Jin Feng

Pavan Nakadi

unread,
Apr 12, 2017, 1:46:43 AM4/12/17
to Pants Developers
How do i create a pants project and run python tests on Intellij Idea on windows platform?. 

when i try to create a pants project by pointing to a BUILD file, Im getting an error 

Mateo Rodriguez(Foursquare)

unread,
Apr 16, 2017, 12:43:17 AM4/16/17
to Pants Developers
Unless something has recently changed, Pants does not support Windows out of the box.

We had an engineer at Foursquare who was successfully developing out of our monorepo while using Windows, but it took a significant amount of inside baseball for him to maintain that system.

- Mateo
Reply all
Reply to author
Forward
0 new messages