2020-02-20 17:53:35 +01:00
|
|
|
# Testing overview
|
2016-07-12 19:49:32 +02:00
|
|
|
|
2020-01-17 09:00:09 +01:00
|
|
|
Zulip takes pride in its extensive, carefully designed test suites.
|
|
|
|
For example, `test-backend` runs a complete test suite (~98% test
|
|
|
|
coverage; 100% on core code) for the Zulip server in under a minute on
|
2021-05-14 00:16:30 +02:00
|
|
|
a fast laptop; very few web apps of similar scope can say something
|
2020-01-17 09:00:09 +01:00
|
|
|
similar.
|
2016-08-18 22:31:42 +02:00
|
|
|
|
2020-01-17 09:00:09 +01:00
|
|
|
This page focused on the mechanics of running automated tests in a
|
|
|
|
[development environment](../development/overview.md); you may also
|
2022-02-24 00:17:21 +01:00
|
|
|
want to read about our [testing philosophy](philosophy.md)
|
2020-01-17 09:00:09 +01:00
|
|
|
and [continuous integration
|
2022-02-24 00:17:21 +01:00
|
|
|
setup](continuous-integration.md).
|
2016-08-18 22:31:42 +02:00
|
|
|
|
2020-01-17 09:00:09 +01:00
|
|
|
Manual testing with a web browser is primarily discussed in the docs
|
|
|
|
on [using the development environment](../development/using.md).
|
2016-11-21 16:36:13 +01:00
|
|
|
|
2016-07-12 19:49:32 +02:00
|
|
|
## Running tests
|
2016-05-15 18:28:38 +02:00
|
|
|
|
2016-06-26 23:55:16 +02:00
|
|
|
Zulip tests must be run inside a Zulip development environment; if
|
2020-01-17 09:00:09 +01:00
|
|
|
you're using Vagrant, you may need to enter it with `vagrant ssh`.
|
2016-06-26 23:55:16 +02:00
|
|
|
|
2020-01-17 09:00:09 +01:00
|
|
|
You can run all of the test suites (similar to our continuous integration)
|
|
|
|
as follows:
|
2016-06-26 23:55:16 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
2016-08-03 01:25:11 +02:00
|
|
|
./tools/test-all
|
|
|
|
```
|
|
|
|
|
2020-01-17 09:00:09 +01:00
|
|
|
However, you will rarely want to do this while actively developing,
|
2021-08-20 21:53:28 +02:00
|
|
|
because it takes a long time. Instead, your edit/refresh cycle will
|
2020-01-17 09:00:09 +01:00
|
|
|
typically involve running subsets of the tests with commands like these:
|
2016-06-26 23:55:16 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
2023-12-05 19:29:58 +01:00
|
|
|
./tools/lint zerver/models/__init__.py # Lint the file you just changed
|
2020-06-27 00:35:15 +02:00
|
|
|
./tools/test-backend zerver.tests.test_markdown.MarkdownTest.test_inline_youtube
|
|
|
|
./tools/test-backend MarkdownTest # Run `test-backend --help` for more options
|
2022-01-28 18:23:50 +01:00
|
|
|
./tools/test-js-with-node util
|
|
|
|
# etc.
|
2016-06-26 23:55:16 +02:00
|
|
|
```
|
2016-07-12 19:49:32 +02:00
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
The commands above will all run in just a few seconds. Many more
|
2024-07-04 12:33:43 +02:00
|
|
|
useful options are discussed in each tool's documentation (e.g.,
|
2020-01-17 09:00:09 +01:00
|
|
|
`./tools/test-backend --help`).
|
|
|
|
|
|
|
|
## Major test suites
|
|
|
|
|
|
|
|
Zulip has a handful of major tests suite that every developer will
|
|
|
|
eventually work with, each with its own page detailing how it works:
|
|
|
|
|
2022-02-24 00:17:21 +01:00
|
|
|
- [Linters](linters.md): Our dozen or so linters run in parallel.
|
|
|
|
- [Django](testing-with-django.md): Server/backend Python tests.
|
|
|
|
- [Node](testing-with-node.md): JavaScript tests for the
|
2020-01-17 09:00:09 +01:00
|
|
|
frontend run via node.js.
|
2022-02-24 00:17:21 +01:00
|
|
|
- [Puppeteer](testing-with-puppeteer.md): End-to-end
|
2020-08-31 03:39:34 +02:00
|
|
|
UI tests run via a Chromium browser.
|
2016-07-12 19:49:32 +02:00
|
|
|
|
2018-04-20 08:44:19 +02:00
|
|
|
## Other test suites
|
|
|
|
|
2020-01-17 09:00:09 +01:00
|
|
|
Additionally, Zulip also has about a dozen smaller tests suites:
|
2018-04-20 08:44:19 +02:00
|
|
|
|
|
|
|
- `tools/test-migrations`: Checks whether the `zerver/migrations`
|
2023-12-05 19:29:58 +01:00
|
|
|
migration content the models defined in `zerver/models/*.py`. See our
|
2019-09-30 19:37:56 +02:00
|
|
|
[schema migration documentation](../subsystems/schema-migrations.md)
|
2018-04-20 08:44:19 +02:00
|
|
|
for details on how to do database migrations correctly.
|
|
|
|
- `tools/test-documentation`: Checks for broken links in this
|
|
|
|
ReadTheDocs documentation site.
|
|
|
|
- `tools/test-help-documentation`: Checks for broken links in the
|
2021-12-17 15:59:58 +01:00
|
|
|
`/help/` help center documentation, and related pages.
|
2018-04-20 08:44:19 +02:00
|
|
|
- `tools/test-api`: Tests that the API documentation at `/api`
|
|
|
|
actually works; the actual code for this is defined in
|
2019-08-04 18:14:48 +02:00
|
|
|
`zerver/openapi/python_examples.py`.
|
2018-04-20 08:44:19 +02:00
|
|
|
- `test-locked-requirements`: Verifies that developers didn't forget
|
|
|
|
to run `tools/update-locked-requirements` after modifying
|
2021-08-20 21:53:28 +02:00
|
|
|
`requirements/*.in`. See
|
2019-09-30 19:37:56 +02:00
|
|
|
[our dependency documentation](../subsystems/dependencies.md) for
|
2018-04-20 08:44:19 +02:00
|
|
|
details on the system this is verifying.
|
|
|
|
- `tools/check-capitalization`: Checks whether translated strings (aka
|
|
|
|
user-facing strings) correctly follow Zulip's capitalization
|
2021-08-20 21:53:28 +02:00
|
|
|
conventions. This requires some maintenance of an exclude list
|
2019-05-27 23:24:26 +02:00
|
|
|
(`tools.lib.capitalization.IGNORED_PHRASES`) of proper nouns
|
|
|
|
mentioned in the Zulip project, but helps a lot in avoiding new
|
|
|
|
strings being added that don't match our style.
|
2018-04-20 08:44:19 +02:00
|
|
|
- `tools/check-frontend-i18n`: Checks for a common bug in Handlebars
|
|
|
|
templates, of using the wrong syntax for translating blocks
|
|
|
|
containing variables.
|
2023-03-04 02:17:54 +01:00
|
|
|
- `./tools/test-run-dev`: Checks that `run-dev` starts properly;
|
2018-04-20 08:44:19 +02:00
|
|
|
this helps prevent bugs that break the development environment.
|
|
|
|
- `./tools/test-queue-worker-reload`: Verifies that Zulip's queue
|
|
|
|
processors properly reload themselves after code changes.
|
2019-01-19 01:16:56 +01:00
|
|
|
- `./tools/setup/optimize-svg`: Checks whether all integration logo SVG
|
|
|
|
graphics are optimized.
|
2018-04-20 08:44:19 +02:00
|
|
|
logos are properly optimized for size (since we're not going to edit
|
|
|
|
third-party logos, this helps keep the Zulip codebase from getting huge).
|
|
|
|
- `./tools/test-tools`: Automated tests for various parts of our
|
|
|
|
development tooling (mostly various linters) that are not used in
|
|
|
|
production.
|
|
|
|
|
|
|
|
Each of these has a reason (usually, performance or a need to do messy
|
|
|
|
things to the environment) why they are not part of the handful of
|
|
|
|
major test suites like `test-backend`, but they all contribute
|
|
|
|
something valuable to helping keep Zulip bug-free.
|
|
|
|
|
2020-01-17 09:00:09 +01:00
|
|
|
## Internet access inside test suites
|
2017-07-04 22:13:38 +02:00
|
|
|
|
|
|
|
As a policy matter, the Zulip test suites should never make outgoing
|
2021-08-20 21:53:28 +02:00
|
|
|
HTTP or other network requests. This is important for 2 major
|
2017-07-04 22:13:38 +02:00
|
|
|
reasons:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Tests that make outgoing Internet requests will fail when the user
|
2017-07-04 22:13:38 +02:00
|
|
|
isn't on the Internet.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Tests that make outgoing Internet requests often have a hidden
|
2017-07-04 22:13:38 +02:00
|
|
|
dependency on the uptime of a third-party service, and will fail
|
|
|
|
nondeterministically if that service has a temporary outage.
|
|
|
|
Nondeterministically failing tests can be a big waste of
|
|
|
|
developer time, and we try to avoid them wherever possible.
|
|
|
|
|
|
|
|
As a result, Zulip's major test suites should never access the
|
2021-08-20 21:53:28 +02:00
|
|
|
Internet directly. Since code in Zulip does need to access the
|
2024-07-04 12:33:43 +02:00
|
|
|
Internet (e.g., to access various third-party APIs), this means that
|
2017-07-04 22:13:38 +02:00
|
|
|
the Zulip tests use mocking to basically hardcode (for the purposes of
|
|
|
|
the test) what responses should be used for any outgoing Internet
|
|
|
|
requests that Zulip would make in the code path being tested.
|
|
|
|
|
|
|
|
This is easy to do using test fixtures (a fancy word for fixed data
|
|
|
|
used in tests) and the `mock.patch` function to specify what HTTP
|
|
|
|
response should be used by the tests for every outgoing HTTP (or other
|
2021-08-20 21:53:28 +02:00
|
|
|
network) request. Consult
|
2022-02-16 01:39:15 +01:00
|
|
|
[our guide on mocking](testing-with-django.md#zulip-mocking-practices) to
|
2017-07-04 22:13:38 +02:00
|
|
|
learn how to mock network requests easily; there are also a number of
|
|
|
|
examples throughout the codebase.
|
|
|
|
|
|
|
|
We partially enforce this policy in the main Django/backend test suite
|
|
|
|
by overriding certain library functions that are used in outgoing HTTP
|
|
|
|
code paths (`httplib2.Http().request`, `requests.request`, etc.) to
|
2021-08-20 21:53:28 +02:00
|
|
|
throw an exception in the backend tests. While this is enforcement is
|
2017-07-04 22:13:38 +02:00
|
|
|
not complete (there a lot of other ways to use the Internet from
|
|
|
|
Python), it is easy to do and catches most common cases of new code
|
2020-10-23 02:43:28 +02:00
|
|
|
depending on Internet access.
|
2017-07-04 22:13:38 +02:00
|
|
|
|
|
|
|
This enforcement code results in the following exception:
|
2017-07-04 13:12:40 +02:00
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```pytb
|
|
|
|
File "tools/test-backend", line 120, in internet_guard
|
|
|
|
raise Exception("Outgoing network requests are not allowed in the Zulip tests."
|
|
|
|
Exception: Outgoing network requests are not allowed in the Zulip tests.
|
|
|
|
...
|
|
|
|
```
|
2016-07-12 19:49:32 +02:00
|
|
|
|
2017-07-04 22:13:38 +02:00
|
|
|
#### Documentation tests
|
|
|
|
|
|
|
|
The one exception to this policy is our documentation tests, which
|
|
|
|
will attempt to verify that the links included in our documentation
|
2021-08-20 21:53:28 +02:00
|
|
|
aren't broken. Those tests end up failing nondeterministically fairly
|
2017-07-04 22:13:38 +02:00
|
|
|
often, which is unfortunate, but there's simply no other correct way
|
2021-08-20 21:53:28 +02:00
|
|
|
to verify links other than attempting to access them. The compromise
|
2020-01-17 09:00:09 +01:00
|
|
|
we've implemented is that in CI, these tests only verify links to
|
2020-06-09 00:58:42 +02:00
|
|
|
websites controlled by the Zulip project (zulip.com, our GitHub,
|
2020-01-17 09:00:09 +01:00
|
|
|
our ReadTheDocs), and not links to third-party websites.
|