tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

index.rst (17332B)


      1 Automated Testing
      2 =================
      3 
      4 You've just written a feature and (hopefully!) want to test it. Or you've
      5 decided that an existing feature doesn't have enough tests and want to contribute
      6 some. But where do you start? You've looked around and found references to things
      7 like "xpcshell" or "web-platform-tests" or "talos". What code, features or
      8 platforms do they all test? Where do their feature sets overlap? In short, where
      9 should your new tests go? This document is a starting point for those who want
     10 to start to learn about Mozilla's automated testing tools and procedures. Below
     11 you'll find a short summary of each framework we use, and some questions to help
     12 you pick the framework(s) you need for your purposes.
     13 
     14 If you still have questions, ask on `Matrix <https://wiki.mozilla.org/Matrix>`__
     15 or on the relevant bug.
     16 
     17 Firefox Production
     18 ------------------
     19 These tests are found within the `mozilla-central <https://hg.mozilla.org/mozilla-central>`__
     20 tree, along with the product code.
     21 
     22 They are run when a changeset is pushed
     23 to `mozilla-central <https://hg.mozilla.org/mozilla-central>`__,
     24 `autoland <https://hg.mozilla.org/integration/autoland/>`__, or
     25 `try </tools/try/index.html>`_, with the results showing up on
     26 `Treeherder <https://treeherder.mozilla.org/>`__. Not all tests will be run on
     27 every changeset; alogrithms are put in place to run the most likely failures,
     28 with all tests being run on a regular basis.
     29 
     30 They can also be run on local builds.
     31 Note: Most of the mobile tests run on emulators, but some of the tests
     32 (notably, performance tests) run on hardware devices.
     33 We try to avoid running mobile tests on hardware devices unnecessarily.
     34 In Treeherder, tests with names that start with "hw-" run on hardware.
     35 
     36 Linting
     37 ~~~~~~~
     38 
     39 Lint tests help to ensure better quality, less error-prone code by
     40 analysing the code with a linter.
     41 
     42 
     43 .. csv-table:: Linters
     44   :header-rows: 1
     45 
     46   "Treeherder Symbol", "Name", "Platforms", "What is Tested"
     47   "``ES``", "`ESLint </code-quality/lint/linters/eslint.html>`__", "All", "JavaScript is analyzed for correctness."
     48   "``ES-build``", "`eslint-build </code-quality/lint/linters/eslint.html#eslint-build-es-b>`_", "All", "Extended javascript analysis that uses build artifacts."
     49   "``mocha(EPM)``", "`ESLint-plugin-mozilla </code-quality/lint/linters/eslint-plugin-mozilla.html>`__", "Desktop", "The ESLint plugin rules."
     50   "``f8``", "`flake8 </code-quality/lint/linters/flake8.html>`__", "All", "Python analyzed for style and correctness."
     51   "``stylelint``", "`Stylelint </code-quality/lint/linters/stylelint.html>`__", "All", "CSS is analyzed for correctness."
     52   "``W``", "`wpt lint </web-platform/index.html>`__", "Desktop", "web-platform-tests analyzed for style and manifest correctness"
     53   "``WR(tidy)``", "`WebRender servo-tidy </testing/webrender/index.html>`__", "Desktop", "Code in gfx/wr is run through servo-tidy."
     54   "``A``", "`Spotless </code-quality/lint/linters/android-format.html>`_", "Android", "Java is analyzed for style and correctness."
     55 
     56 .. _Functional_testing:
     57 
     58 Functional testing
     59 ~~~~~~~~~~~~~~~~~~
     60 
     61 .. csv-table:: Automated Test Suites
     62   :header-rows: 2
     63 
     64   "Treeherder Symbol", "Name", "Platform", "Process", "Environment", "", "Privilege", "What is Tested"
     65   "", "", "", "", "Shell", "Browser Profile", "",
     66   "``R(J)``", "JS Reftest", "Desktop", "N/A", "JSShell", "N/A", "N/A", "The JavaScript engine's implementation of the JavaScript language."
     67   "``R(C)``", "`Crashtest </web-platform/index.html>`__", "All", "Child", "Content", "Yes", "Low", "That pages load without crashing, asserting, or leaking."
     68   "``R(R)``", "`Reftest </web-platform/index.html>`__", "All", "Child", "Content", "Yes", "Low", "That pages are rendered (and thus also layed out) correctly."
     69   "``GTest``", "`GTest </gtest/index.html>`__", "All", "N/A", "Terminal", "N/A", "N/A", "Code that is not exposed to JavaScript."
     70   "``X``", "`xpcshell </testing/xpcshell/index.html>`__", "All", "Parent, Allow", "XPCShell", "Allow", "High", "Low-level code exposed to JavaScript, such as XPCOM components."
     71   "``M(a11y)``", "Accessibility (mochitest-a11y)", "Desktop", "Child", "Content", "Yes", "?", "Accessibility interfaces."
     72   "``M(1), M(2), M(...)``", "`Mochitest plain </testing/mochitest-plain/index.html>`__", "All", "Child", "Content", "Yes", "Low, Allow", "Features exposed to JavaScript in web content, like DOM and other Web APIs, where the APIs do not require elevated permissions to test."
     73   "``M(c1/c2/c3)``", "`Mochitest chrome </testing/chrome-tests/index.html>`__", "All", "Child, Allow", "Content", "Yes", "High", "Code requiring UI or JavaScript interactions with privileged objects."
     74   "``M(bc)``", "`Mochitest browser-chrome </testing/mochitest-plain/index.html>`__", "All", "Parent, Allow", "Browser", "Yes", "High", "How the browser UI interacts with itself and with content."
     75   "``M(remote)``", "Mochitest Remote Protocol", "All", "Parent, Allow", "Browser", "Yes", "High", "Firefox Remote Protocol (Implements parts of Chrome dev-tools protocol). Based on Mochitest browser-chrome."
     76   "``SM(...), SM(pkg)``", "`SpiderMonkey automation <https://wiki.mozilla.org/Javascript:Automation_Builds>`__", "Desktop", "N/A", "JSShell", "N/A", "Low", "SpiderMonkey engine shell tests and JSAPI tests."
     77   "``W``", "`web-platform-tests </web-platform/index.html>`__", "Desktop", "Child", "Content", "Yes", "Low", "Standardized features exposed to ECMAScript in web content; tests are shared with other vendors."
     78   "``Wr``", "`web-platform-tests <https://web-platform-tests.org/writing-tests/reftests.html>`__", "All", "Child", "Content", "Yes", "Low", "Layout and graphic correctness for standardized features; tests are shared with other vendors."
     79   "``Mn``", "`Marionette </testing/marionette/Testing.html>`__", "Desktop", "?", "Content, Browser", "?", "High", "Large out-of-process function integration tests and tests that do communication with multiple remote Gecko processes."
     80   "``Fxfn``", "`Firefox UI Tests </remote/Testing.html#puppeteer-tests>`__", "Desktop", "?", "Content, Browser", "Yes", "High", "Integration tests with a focus on the user interface and localization."
     81   "``tt(c)``", "`telemetry-tests-client </toolkit/components/telemetry/internals/tests.html>`__", "Desktop", "N/A", "Content, Browser", "Yes", "High", "Integration tests for the Firefox Telemetry client."
     82   "``TV``", "`Test Verification (test-verify) </testing/test-verification/index.html>`__", "All", "Depends on test harness", "?", "?", "?", "Uses other test harnesses - mochitest, reftest, xpcshell - to perform extra testing on new/modified tests."
     83   "``TVw``", "`Test Verification for wpt (test-verify-wpt) </testing/test-verification/index.html>`__", "Desktop", "Child", "?", "?", "?", "Uses wpt test harnesses to perform extra testing on new/modified web-platform tests."
     84   "``WR(wrench)``", "`WebRender standalone tests </testing/webrender/index.html>`__", "All", "N/A", "Terminal", "N/A", "N/A", "WebRender rust code (as a standalone module, with Gecko integration)."
     85 
     86 Note: there are preference-based variations of the previous testing suites.
     87 For example, mochitests on Treeherder can have ``gli``, ``swr``, ``spi``,
     88 ``nofis``, ``a11y-checks``, ``spi-nw-1proc``, and many others. Another
     89 example is GTest, which can use ``GTest-1proc``. To learn more about
     90 these variations, you can mouse hover over these items to read a
     91 description of what these abbreviations mean.
     92 
     93 .. _Table_key:
     94 
     95 Table key
     96 ^^^^^^^^^
     97 
     98 Symbol
     99   Abbreviation for the test suite used by
    100   `Treeherder <https://treeherder.mozilla.org/>`__. The first letter
    101   generally indicates which of the test harnesses is used to execute
    102   the test. The letter in parentheses identifies the actual test suite.
    103 Name
    104   Common name used when referring to the test suite.
    105 Platform
    106   Most test suites are supported only on a subset of the available
    107   plaforms and operating systems. Unless otherwise noted:
    108 
    109   -  **Desktop** tests run on Windows, Mac OS X, and Linux.
    110   -  **Mobile** tests run on Android emulators or remotely on Android
    111      devices.
    112 
    113 Process
    114   -  When **Parent** is indicated, the test file will always run in the
    115      parent process, even when the browser is running in Electrolysis
    116      (e10s) mode.
    117   -  When **Child** is indicated, the test file will run in the child
    118      process when the browser is running in Electrolysis (e10s) mode.
    119   -  The **Allow** label indicates that the test has access to
    120      mechanisms to run code in the other process.
    121 
    122 Environment
    123   -  The **JSShell** and **XPCShell** environments are limited
    124      JavaScript execution environments with no windows or user
    125      interface (note however that XPCShell tests on Android are run
    126      within a browser window.)
    127   -  The **Content** indication means that the test is run inside a
    128      content page loaded by a browser window.
    129   -  The **Browser** indication means that the test is loaded in the
    130      JavaScript context of a browser XUL window.
    131   -  The **Browser Profile** column indicates whether a browser profile
    132      is loaded when the test starts. The **Allow** label means that the
    133      test can optionally load a profile using a special command.
    134 
    135 Privilege
    136   Indicates whether the tests normally run with low (content) or high
    137   (chrome) JavaScript privileges. The **Allow** label means that the
    138   test can optionally run code in a privileged environment using a
    139   special command.
    140 
    141 .. _Performance_testing:
    142 
    143 Performance testing
    144 ~~~~~~~~~~~~~~~~~~~
    145 
    146 There are many test harnesses used to test performance.
    147 `For more information on the various performance harnesses,
    148 check out the perf docs. </testing/perfdocs>`_
    149 
    150 
    151 .. _So_which_should_I_use:
    152 
    153 So which should I use?
    154 ----------------------
    155 
    156 Generally, you should pick the lowest-level framework that you can. If
    157 you are testing JavaScript but don't need a window, use XPCShell or even
    158 JSShell. If you're testing page layout, try to use
    159 `web-platform-test reftest.
    160 <https://web-platform-tests.org/writing-tests/reftests.html>`_
    161 The advantage in lower level testing is that you don't drag in a lot of
    162 other components that might have their own problems, so you can home in
    163 quickly on any bugs in what you are specifically testing.
    164 
    165 Here's a series of questions to ask about your work when you want to
    166 write some tests.
    167 
    168 .. _Is_it_low-level_code:
    169 
    170 Is it low-level code?
    171 ~~~~~~~~~~~~~~~~~~~~~
    172 
    173 If the functionality is exposed to JavaScript, and you don't need a
    174 window, consider `XPCShell </testing/xpcshell/index.html>`__. If not,
    175 you'll probably have to use `GTest </gtest/index.html>`__, which can
    176 test pretty much anything. In general, this should be your
    177 last option for a new test, unless you have to test something that is
    178 not exposed to JavaScript.
    179 
    180 .. _Does_it_cause_a_crash:
    181 
    182 Does it cause a crash?
    183 ~~~~~~~~~~~~~~~~~~~~~~
    184 
    185 If you've found pages that crash Firefox, add a
    186 `crashtest </web-platform/index.html>`__ to
    187 make sure future versions don't experience this crash (assertion or
    188 leak) again. Note that this may lead to more tests once the core
    189 problem is found.
    190 
    191 .. _Is_it_a_layoutgraphics_feature:
    192 
    193 Is it a layout/graphics feature?
    194 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    195 
    196 `Reftest </layout/Reftest.html#writing-tests>`__ is your best bet, if possible.
    197 
    198 .. _Do_you_need_to_verify_performance:
    199 
    200 Do you need to verify performance?
    201 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    202 
    203 `Use an appropriate performance test suite from this list </testing/perfdocs>`_.
    204 
    205 .. _Are_you_testing_UI_features:
    206 
    207 Are you testing UI features?
    208 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    209 
    210 Try one of the flavors of
    211 `mochitest </testing/mochitest-plain/index.html>`__, or
    212 `Marionette </docs/Marionette>`__ if the application also needs to be
    213 restarted, or tested with localized builds.
    214 
    215 .. _Are_you_testing_MobileAndroid:
    216 
    217 Are you testing Mobile/Android?
    218 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    219 
    220 If you are testing GeckoView, you will need to need to use
    221 `JUnit integration tests
    222 </mobile/android/geckoview/contributor/junit.html#testing-overview>`__.
    223 
    224 There are some specific features that
    225 `Mochitest </testing/mochitest-plain/index.html>`__ or
    226 `Reftest </layout/Reftest.html>`__ can cover. Browser-chrome tests do not run on
    227 Android. If you want to test performance, `Raptor </testing/perfdocs/raptor.html>`__ will
    228 be a good choice.
    229 
    230 
    231 .. _Are_you_doing_none_of_the_above:
    232 
    233 Are you doing none of the above?
    234 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    235 
    236 -  To get your tests running in continuous integration, try
    237   `web-platform-tests </web-platform/index.html>`_, or
    238   `Mochitest </testing/mochitest-plain/index.html>`__, or,
    239   if higher privileges are required, try
    240   `Mochitest browser chrome tests </testing/mochitest-plain/index.html>`__.
    241 -  For Desktop Firefox, or if you just want to see the future of Gecko
    242   testing, look into the on-going
    243   `Marionette </testing/marionette/Testing.html#harness-tests>`__ project.
    244 
    245 .. _Need_to_get_more_data_out_of_your_tests:
    246 
    247 Need to get more data out of your tests?
    248 ----------------------------------------
    249 
    250 Most test jobs now expose an environment variable named
    251 ``MOZ_UPLOAD_DIR``. If this variable is set during automated test runs,
    252 you can drop additional files into this directory, and they will be
    253 uploaded to a web server when the test finishes. The URLs to retrieve
    254 the files will be output in the test log.
    255 
    256 Passing ``MOZ_RECORD_TEST=1`` as an environment variable when running some
    257 tests (e.g. mochitests) on Linux Desktop and macOS will trigger a recording of the
    258 desktop with GNOME Screencast. This works on try as well, in which case the video
    259 file will be uploaded as an artifact and available in the
    260 ``Artifacts and Debugging Tools`` panel on Treeherder.
    261 
    262 For browser chrome mochitests, passing ``MOZ_DEVTOOLS_TEST_SCOPES=1`` as an
    263 environment variable will record all variables and arguments available in
    264 the scope of the test when any assert fails. On try, each failed assert will generate
    265 a JSON file named `scope-variables-[...].json` which will be uploaded as a
    266 test artifact. When using the feature locally, set MOZ_UPLOAD_DIR to a local
    267 folder where the JSON files should be saved. Note that Firefox opens JSON files
    268 with the built-in DevTools JSON viewer.
    269 
    270 .. _Need_to_set_preferences_for_test-suites:
    271 
    272 Need to set preferences for test-suites?
    273 ----------------------------------------
    274 
    275 First ask yourself if these prefs need to be enabled for all tests or
    276 just a subset of tests (e.g to enable a feature).
    277 
    278 .. _Setting_prefs_that_only_apply_to_certain_tests:
    279 
    280 Setting prefs that only apply to certain tests
    281 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    282 
    283 If the answer is the latter, try to set the pref as local to the tests
    284 that need it as possible. Here are some options:
    285 
    286 -  If the test runs in chrome scope (e.g mochitest chrome or
    287   browser-chrome), you can use
    288   `Services.prefs
    289   <https://searchfox.org/mozilla-central/source/modules/libpref/nsIPrefBranch.idl>`__
    290   to set the prefs in your test's setup function. Be sure to reset the
    291   pref back to its original value during teardown!
    292 
    293 -  Mochitest plain tests can use
    294   `SpecialPowers
    295   </testing/mochitest-plain/faq.html#what-if-i-need-to-change-a-preference-to-run-my-test>`__
    296   to set prefs.
    297 
    298 -  All variants of mochitest can set prefs in their manifests. For
    299   example, to set a pref for all tests in a manifest:
    300 
    301   ::
    302 
    303      [DEFAULT]
    304      prefs =
    305        my.awesome.pref=foo,
    306        my.other.awesome.pref=bar,
    307      [test_foo.js]
    308      [test_bar.js]
    309 
    310 -  All variants of reftest can also set prefs in their
    311   `manifests </layout/Reftest.html>`__.
    312 
    313 -  All variants of web-platform-tests can also `set prefs in their
    314   manifests </web-platform/index.html#enabling-prefs>`__.
    315 
    316 .. _Setting_prefs_that_apply_to_the_entire_suite:
    317 
    318 Setting prefs that apply to the entire suite
    319 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    320 
    321 Most test suites define prefs in user.js files that live under
    322 `testing/profiles
    323 <https://searchfox.org/mozilla-central/source/testing/profiles>`__.
    324 Each directory is a profile that contains a ``user.js`` file with a
    325 number of prefs defined in it. Test suites will then merge one or more
    326 of these basic profiles into their own profile at runtime. To see which
    327 profiles apply to which test suites, you can inspect
    328 `testing/profiles/profiles.json
    329 <https://searchfox.org/mozilla-central/source/testing/profiles/profiles.json>`__.
    330 Profiles at the beginning of the list get overridden by profiles at the
    331 end of the list.
    332 
    333 Because this system makes it hard to get an overall view of which
    334 profiles are set for any given test suite, a handy ``profile`` utility
    335 was created:
    336 
    337 ::
    338 
    339   $ cd testing/profiles
    340   $ ./profile -- --help
    341   usage: profile [-h] {diff,sort,show,rm} ...
    342   $ ./profile show mochitest          # prints all prefs that will be set in mochitest
    343   $ ./profile diff mochitest reftest  # prints differences between the mochitest and reftest suites
    344 
    345 .. container:: blockIndicator note
    346 
    347   **Note:** JS engine tests do not use testing/profiles yet, instead
    348   `set prefs
    349   here <https://searchfox.org/mozilla-central/source/js/src/tests/user.js>`__.
    350 
    351 Adding New Context to Skip Conditions
    352 -------------------------------------
    353 
    354 Often when standing up new test configurations, it's necessary to add new keys
    355 that can be used in ``skip-if`` annotations.
    356 
    357 .. toctree::
    358 
    359   manifest-sandbox