code: def test_metric_tracking(self):
      class LayerWithMetric(layers.Layer):
          def __init__(self, units):
              super().__init__()
              self.dense = layers.Dense(units)
              self.metric = metrics.MeanSquaredError(name="my_metric")
  
          def build(self, input_shape):
              self.dense.build(input_shape)
  
          def call(self, x):
              return self.dense(x)
  
      class NestedLayerWithMetric(layers.Layer):
          def __init__(self, units):
              super().__init__()
              self.layer_with_metric = LayerWithMetric(units)
              self.metric = metrics.MeanSquaredError(name="my_metric")
  
          def build(self, input_shape):
              self.layer_with_metric.build(input_shape)
  
          def call(self, x):
              return self.layer_with_metric(x)
  
      layer = LayerWithMetric(3)
      layer.build((1, 3))
  
      self.assertLen(layer.metrics_variables, 2)
      self.assertLen(layer.trainable_variables, 2)
      self.assertLen(layer.non_trainable_variables, 0)
  
      layer = NestedLayerWithMetric(3)
      layer.build((1, 3))
  
      self.assertLen(layer.metrics_variables, 4)
      self.assertLen(layer.trainable_variables, 2)
      self.assertLen(layer.non_trainable_variables, 0)
  
imports: import numpy
  import pytest
  from keras import backend
  from keras import layers
  from keras import metrics
  from keras import models
  from keras import ops
  from keras import testing
label_desc: Use image node_id 5 for calling the LayerTest obj's underlying member method code with example usage: obj.test_metric_tracking() and returns: self, self