Closed Bug 1472199 Opened 4 years ago Closed 4 years ago

Set the root directory for Python unit tests to topsrcdir

Categories

(Testing :: Python Test, enhancement)

Version 2
enhancement
Not set
normal

Tracking

(firefox63 fixed)

RESOLVED FIXED
mozilla63
Tracking Status
firefox63 --- fixed

People

(Reporter: davehunt, Assigned: davehunt, Mentored)

References

Details

(Keywords: good-first-bug)

Attachments

(2 files)

Currently the root directory for the tests executed via |mach python-test| is determined using pytest default strategy [1]. This means that it's often the common ancestor of the tests, and as the nodeids is constructed from the root directory, were unable to uniquely identify tests from different parts of the source code.

pytest 3.5 introduced a `--rootdir` command line option for overriding the root directory. We should use this to set the root directory to the top of the source tree, so all tests will have an absolute path and unique nodeid.

[1] https://docs.pytest.org/en/latest/customize.html#finding-the-rootdir
Depends on: 1472201
Assignee: nobody → dave.hunt
Status: NEW → ASSIGNED
You can see the rootdir in the pytest header.

Wthout the patch running |mach python-test mozfile| shows a rootdir for me of /Users/dhunt/workspace/firefox/testing/mozbase/mozfile/tests, which means an example test node of test_extract.py::TestExtract::test_extract.

With the patch shows a rootdir for me of /Users/dhunt/workspace/firefox, which means the same test node id would be testing/mozbase/mozfile/tests/test_extract.py::TestExtract::test_extract.

Note that the standard output from pytest shows the relative path to the test, however the mozlog pytest plugin uses the node ids, so the issue of tests not being uniquely identifiable only applies to any files created by mozlog.
Blocks: 1422344
Comment on attachment 8989134 [details]
Bug 1472199 - Set the root directory for Python unit tests to topsrcdir;

https://reviewboard.mozilla.org/r/254194/#review261308

::: config/mozunit/mozunit/mozunit.py:22
(Diff revision 1)
> +except ImportError:
> +    import buildconfig
> +    topsrcdir = buildconfig.topsrcdir

This will fail if you don't have a local build of Firefox which is bad because many of the python tests don't need a build. Instead use:

    from mozbuild.base import MozbuildObject
    build = MozbuildObject.from_environment(cwd=here)
    build.topsrcdir

I'm not sure if `mozbuild.base` is python 3 compatible or not, but would be worth checking (and if it isn't it would be a good candidate to prioritize).
Attachment #8989134 - Flags: review?(ahal) → review+
Comment on attachment 8989134 [details]
Bug 1472199 - Set the root directory for Python unit tests to topsrcdir;

https://reviewboard.mozilla.org/r/254194/#review261308

> This will fail if you don't have a local build of Firefox which is bad because many of the python tests don't need a build. Instead use:
> 
>     from mozbuild.base import MozbuildObject
>     build = MozbuildObject.from_environment(cwd=here)
>     build.topsrcdir
> 
> I'm not sure if `mozbuild.base` is python 3 compatible or not, but would be worth checking (and if it isn't it would be a good candidate to prioritize).

Unfortunately `mozbuild.base` depends on `mach`, which depends on `mozprocess`. I'll raise bugs for adding Python 3 support to mach and mozbase.
Pushed by dhunt@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/530f89f70093
Set the root directory for Python unit tests to topsrcdir; r=ahal
Flags: needinfo?(dave.hunt)
Comment on attachment 8989998 [details]
Bug 1472199 - [mozbuild] Remove unittest dependency from test_pythonutil.py and narrow to tests module;

https://reviewboard.mozilla.org/r/255020/#review261972

Lgtm!
Attachment #8989998 - Flags: review?(ahal) → review+
Pushed by dhunt@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/565f27e6e753
[mozbuild] Remove unittest dependency from test_pythonutil.py and narrow to tests module; r=ahal
https://hg.mozilla.org/integration/autoland/rev/9da7f0f1cb6d
Set the root directory for Python unit tests to topsrcdir; r=ahal
https://hg.mozilla.org/mozilla-central/rev/565f27e6e753
https://hg.mozilla.org/mozilla-central/rev/9da7f0f1cb6d
Status: ASSIGNED → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla63
You need to log in before you can comment on or make changes to this bug.