You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_metric.py 766 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import pytest
  2. from fastNLP import Metric
  3. class DemoMetric(Metric):
  4. def __init__(self):
  5. super().__init__(backend='torch')
  6. self.count = 0
  7. self.register_element('count', 0)
  8. def evaluate(self):
  9. self.count += 1
  10. print(self.count)
  11. class DemoMetric1(Metric):
  12. def __init__(self):
  13. super().__init__(backend='torch')
  14. self.register_element('count', 0)
  15. self.count = 2
  16. def evaluate(self):
  17. self.count += 1
  18. return self.count
  19. @pytest.mark.torch
  20. class TestMetric:
  21. def test_v1(self):
  22. with pytest.raises(RuntimeError):
  23. dmtr = DemoMetric()
  24. dmtr.evaluate()
  25. def test_v2(self):
  26. dmtr = DemoMetric1()
  27. assert 3 == dmtr.evaluate()