Implementation:Tensorflow Serving Classifier Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Classification |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
Test suite validating the TensorFlow Serving classifier servable, which handles classification inference requests.
Description
This test file provides comprehensive coverage of the TensorFlowClassifier implementation. It uses a FakeSession that simulates a TensorFlow session by parsing serialized Example protos from input tensors and producing classification classes and scores. The RegressorTest parameterized fixture (parameterized on method name checking) validates classification with example lists, context, named signatures, error handling for malformed inputs, and correctness of tensor shapes and types.
Key areas tested include:
- Classification with example lists and example lists with context
- Classes-only and scores-only classification outputs
- Named and invalid signature handling
- Error cases: empty inputs, malformed scores, incorrect tensor batch sizes, incorrect tensor types, mismatched class counts
- Method name checking feature toggle
Usage
Run these tests to validate changes to the classifier servable, signature handling, or input/output tensor processing for classification tasks.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File: tensorflow_serving/servables/tensorflow/classifier_test.cc
- Lines: 1-1018
Test Fixture
class FakeSession : public tensorflow::Session {
public:
explicit FakeSession(absl::optional<int64_t> expected_timeout)
: expected_timeout_(expected_timeout) {}
~FakeSession() override = default;
// Parses serialized Examples from input tensors and produces
// classification classes and scores as output tensors.
absl::Status Run(const RunOptions& run_options,
const std::vector<std::pair<string, Tensor>>& inputs,
const std::vector<string>& output_names,
const std::vector<string>& target_nodes,
std::vector<Tensor>* outputs,
RunMetadata* run_metadata,
const thread::ThreadPoolOptions& thread_pool_options) override;
};
class ClassifierTest : public ::testing::TestWithParam<bool> {
// Parameterized on method name check enabled/disabled
};
Build Target
bazel test //tensorflow_serving/servables/tensorflow:classifier_test
Test Coverage
Key Test Cases
| Test Name | Category | Description |
|---|---|---|
| ExampleList | Input Handling | Tests classification with a standard example list |
| ExampleListWithContext | Input Handling | Tests classification with example list and context features |
| ExampleListWithContext_DuplicateFeatures | Input Handling | Tests context feature merging with duplicate features |
| ClassesOnly | Output Handling | Tests classification returning only class labels |
| ScoresOnly | Output Handling | Tests classification returning only scores |
| ZeroScoresArePresent | Output Handling | Verifies zero-valued scores are correctly included |
| ValidNamedSignature | Signature | Tests classification with a valid named signature |
| InvalidNamedSignature | Error Handling | Tests error on invalid named signature |
| MalformedScores | Error Handling | Tests error on improperly sized score tensors |
| MissingClassificationSignature | Error Handling | Tests error when classification signature is missing |
| EmptyInput | Error Handling | Tests error on empty input |
| EmptyExampleList | Error Handling | Tests error on empty example list |
| EmptyExampleListWithContext | Error Handling | Tests error on empty example list with context |
| RunsFails | Error Handling | Tests handling of session Run failures |
| ClassesIncorrectTensorBatchSize | Validation | Tests error on mismatched class tensor batch size |
| ClassesIncorrectTensorType | Validation | Tests error on incorrect class tensor type |
| ScoresIncorrectTensorBatchSize | Validation | Tests error on mismatched score tensor batch size |
| ScoresIncorrectTensorType | Validation | Tests error on incorrect score tensor type |
| MismatchedNumberOfTensorClasses | Validation | Tests error when class and score tensor sizes differ |
| MethodNameCheck | Feature Toggle | Tests method name checking feature |
Usage Examples
Test Pattern
TEST_P(ClassifierTest, ExampleList) {
auto* examples =
request_.mutable_input()->mutable_example_list()->mutable_examples();
*examples->Add() = example_with_class({{"car", "train", "bicycle"}});
*examples->Add() = example_with_class({{"car", "train", "bicycle"}});
ClassificationResult result;
TF_ASSERT_OK(classifier_->Classify(request_, &result));
EXPECT_THAT(result, EqualsProto(expected_result));
}