allennlp.common.testing

Utilities and helpers for writing tests.

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

Bases: unittest.case.TestCase

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/agent2/work/8feb324ce7c68d53/allennlp/tests/fixtures')
MODULE_ROOT = PosixPath('/local/deploy/agent2/work/8feb324ce7c68d53/allennlp')
PROJECT_ROOT = PosixPath('/local/deploy/agent2/work/8feb324ce7c68d53')
TESTS_ROOT = PosixPath('/local/deploy/agent2/work/8feb324ce7c68d53/allennlp/tests')
TOOLS_ROOT = PosixPath('/local/deploy/agent2/work/8feb324ce7c68d53/allennlp/tools')
setUp()[source]
tearDown()[source]
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()[source]
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]
Parameters:
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]