I have a class A that initializes a Counter in its init
from prometheus_client import Counter
class A:
def __init__(self):
self.my_counter = Counter('an_awesome_counter')
def method_1(self):
return 1
def method_2(self):
return 2
Then I write test class :
import unittest
import A
class ATests(unittest.TestCase):
def setUp(self):
self.a = A()
def tearDown(self):
self.a = None
def method_1_test(self):
....
def method_2_test(self):
....
Thing is, if I run the test separately, they are fine. Yet when I run them together (Run the whole ATests class), I have the error as:
ValueError: Duplicated timeseries in CollectorRegistry: {'an_awesome_counter'}
So it seems that the python environment isn't reset after each test run. I check the CollectorRegistry and there is a method to unregister collector, but it seems a bit ugly to do that.
I wonder if there is another way to solve this problem? Like forcing the test to run with new environment every time for example..
Thank you.
For the moment, I moved a = A()
out of setup(self) and turned it into a class variable as a workaround solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With