Python Test Runner

Python Test Runner (ptr) was born to run tests in an opinionated way, within arbitrary code repositories. Ptr supports many Python projects with unit tests defined in their setup. (cfg py) files per repository. Ptr allows developers to test multiple projects/modules in one Python environment through the use of a single test virtual environment.

  • VS Code also shows test results in the Python Test Log output panel. Run tests in parallel. Support for running tests in parallel with pytest is available through the pytest-xdist package. To enable parallel testing: Open the integrated terminal and install the pytest-xdist package. For more details, refer to the project's documentation page.
  • !python -m unittest testvolumecuboid.py. Ran 1 test in 0.000s OK As you can see by changing the testvolume method name to volume, only 1 test was run, i.e., testinputvalue. Hence, it is important to name your testing methods with a test keyword at the beginning.

Injection¶

With either of the two methods mentioned above, the variables TEST_APPand TEST_METHOD will be injected into the fake WSGI environment.

TEST_APP will contain a secret key that you define when executing your tests.TEST_METHOD will contain the method used to execute the models. This will eitherbe WebTest if you use the webtest method, or FakeEnv if you use the new_envmethod.

Install python interpreter windows 10

From your model, you can verify the existence of this key and optionally performlogic checks based on this, such as defining a test database as your main objectinstead of using your production database.

Here is an example of determining the existence of this key.:

Warning

You might want to remove these checks in a production environment. The secretkey is there to provide some security in case you forget, but the check doescreate a little overhead.

Note

This is the recommended way of using a test database. There are tworeasons.

  1. If you have quite a few functions declared in your models thatrely on your db object, if you want to test these functions, theywill break if you use the copy_db function, since they will referto your actual database in this case.
  2. The copy_db function introduces lag time into your testing, sinceit will effectively be creating a brand new database on eachtest function.

Another approach is to use the provided copy_db function, which will make asqlite:memory: DAL instance using the tables provided by your main db object.More info on this in the Fake Environment section.

Package/Directory-level fixtures (setups)¶

If you have nested test directories, you can have per-directory fixture scopesby placing fixture functions in a conftest.py file in that directoryYou can use all types of fixtures including autouse fixtures which are the equivalent of xUnit’s setup/teardownconcept. It’s however recommended to have explicit fixture references in yourtests or test classes rather than relying on implicitly executingsetup/teardown functions, especially if they are far away from the actual tests.

Here is an example for making a db fixture available in a directory:

and then a test module in that directory:

another test module:

and then a module in a sister directory which will not seethe db fixture:

We can run this:

Python Test Runner Jobs

The two test modules in the a directory see the same db fixture instancewhile the one test in the sister-directory b doesn’t see it. We could of coursealso define a db fixture in that sister directory’s conftest.py file.Note that each fixture is only instantiated if there is a test actually needingit (unless you use “autouse” fixture which are always executed ahead of the first testexecuting).