code: def test_add_loss(self):
      class LossLayer(layers.Layer):
          def call(self, x):
              self.add_loss(ops.sum(x))
              return x
  
      layer = LossLayer()
      layer(np.ones((1,)))
      self.assertLen(layer.losses, 1)
      self.assertAllClose(layer.losses[0], 1.0)
  
      # losses are reset upon call
      layer = LossLayer()
      layer(np.ones((1,)))
      self.assertLen(layer.losses, 1)
      self.assertAllClose(layer.losses[0], 1.0)
  
      # It works inside a model
      model = models.Sequential([layer])
      model(np.ones((1,)))
      self.assertLen(model.losses, 1)
      self.assertAllClose(model.losses[0], 1.0)
  
      # It works recursively in nested models
      model = models.Sequential([model])
      model(np.ones((1,)))
      self.assertLen(model.losses, 1)
      self.assertAllClose(model.losses[0], 1.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 8 for calling the LayerTest obj's underlying member method code with example usage: obj.test_add_loss() and returns: x