Utilities and helpers for writing tests.

class allennlp.common.testing.test_case.AllenNlpTestCase(methodName='runTest')[source]


A custom subclass of TestCase that disables some of the more verbose AllenNLP logging and that creates and destroys a temp directory as a test fixture.

FIXTURES_ROOT = PosixPath('/local/deploy/agent1/work/8feb324ce7c68d53/allennlp/tests/fixtures')
MODULE_ROOT = PosixPath('/local/deploy/agent1/work/8feb324ce7c68d53/allennlp')
PROJECT_ROOT = PosixPath('/local/deploy/agent1/work/8feb324ce7c68d53')
TESTS_ROOT = PosixPath('/local/deploy/agent1/work/8feb324ce7c68d53/allennlp/tests')
TOOLS_ROOT = PosixPath('/local/deploy/agent1/work/8feb324ce7c68d53/allennlp/tools')
class allennlp.common.testing.model_test_case.ModelTestCase(methodName='runTest')[source]

Bases: allennlp.common.testing.test_case.AllenNlpTestCase

A subclass of AllenNlpTestCase with added methods for testing Model subclasses.

assert_fields_equal(field1, field2, name: str, tolerance: float = 1e-06) → None[source]
static check_model_computes_gradients_correctly(model: allennlp.models.model.Model, model_batch: typing.Dict[str, typing.Union[typing.Any, typing.Dict[str, typing.Any]]], params_to_ignore: typing.Set[str] = None)[source]
ensure_batch_predictions_are_consistent(keys_to_ignore: typing.Iterable[str] = ())[source]

Ensures that the model performs the same on a batch of instances as on individual instances. Ignores metrics matching the regexp .*loss.* and those specified explicitly.

keys_to_ignore : Iterable[str], optional (default=())

Names of metrics that should not be taken into account, e.g. “batch_weight”.

ensure_model_can_train_save_and_load(param_file: str, tolerance: float = 0.0001, cuda_device: int = -1, gradients_to_ignore: typing.Set[str] = None, overrides: str = '')[source]
param_file : str

Path to a training configuration file that we will use to train the model for this test.

tolerance : float, optional (default=1e-4)

When comparing model predictions between the originally-trained model and the model after saving and loading, we will use this tolerance value (passed as rtol to numpy.testing.assert_allclose).

cuda_device : int, optional (default=-1)

The device to run the test on.

gradients_to_ignore : Set[str], optional (default=None)

This test runs a gradient check to make sure that we’re actually computing gradients for all of the parameters in the model. If you really want to ignore certain parameters when doing that check, you can pass their names here. This is not recommended unless you’re really sure you don’t need to have non-zero gradients for those parameters (e.g., some of the beam search / state machine models have infrequently-used parameters that are hard to force the model to use in a small test).

overrides : str, optional (default = “”)

A JSON string that we will use to override values in the input parameter file.

set_up_model(param_file, dataset_file)[source]