0

I've written a module called Consumer.py, containing a class (Consumer). This class is initialized using a configuration file thay contains different parameters it uses for computation and the name of a loq que used for logging.

I want to write unit tests for this class so i've made a script called test_Consumer.py with a class called TestConsumerMethods(unittest.TestCase).

Now, what i've done is create a new object of the Consumer class called cons, and then i use that to call on the class methods for testing. For example, Consumer has a simple method that checks if a file exists in a given directory. The test i've made looks like this

import Consumer
from Consumer import Consumer
cons = Consumer('mockconfig.config', 'logque1')
class TestConsumerMethods(unittest.TestCase):
 def test_fileExists(self):
 self.assertEqual(cons.file_exists('./dir/', 'thisDoesntExist.config), False)
 self. assertEqual(cons.file_exists('./dir/', thisDoesExist.config), True)

Is this the correct way to test my class? I mean, ideally i'd like to just use the class methods without having to instantiate the class because to "isolate" the code, right?

asked Jul 26, 2016 at 11:34

2 Answers 2

2

Don't make a global object to test against, as it opens up the possibility that some state will get set on it by one test, and affect another.

Each test should run in isolation and be completely independent from others.

Instead, either create the object in your test, or have it automatically created for each test by putting it in the setUp method:

import Consumer
from Consumer import Consumer
class TestConsumerMethods(unittest.TestCase):
 def setUp(self):
 self.cons = Consumer('mockconfig.config', 'logque1')
 def test_fileExists(self):
 self.assertEqual(self.cons.file_exists('./dir/', 'thisDoesntExist.config), False)
 self. assertEqual(self.cons.file_exists('./dir/', thisDoesExist.config), True)

As far as whether you actually have to instantiate your class at all, that depends on the implementation of the class. I think generally you'd expect to instantiate a class to test its methods.

answered Jul 26, 2016 at 12:07
Sign up to request clarification or add additional context in comments.

2 Comments

That makes sense, thanks! Is it necessary to delete the object in tearDown() aswell?
@Zazz Typically, no. TestConsumerMethods will be instantiated for each test, and each time it will construct a new Consumer. The old consumer will just be garbage collected like anything else that goes out of scope. You only need to do something in tearDown if you need to explicitly free some resource or other.
0

I'm not sure if that's what you're searching for, but you could add your tests at the end of your file like this :

#!/usr/bin/python
...
class TestConsumerMethods(...):
 ...
if __name__ == "__main__":
 # add your tests here.

This way, by executing the file containing the class definition, you execute the tests you put in the if statement.

This way the tests will only be executed if you directly execute the file itself, but not if you import the class from it.

answered Jul 26, 2016 at 12:01

2 Comments

The problem with this, is that then that's the only way you can run tests. You can't discover them with unittest, or easily run them in a CI setup
@SpoonMeiser That's right, indeed, but that could be a good way if you need a simple way to implement some tests (no unittest knowledge required for instance). But that's still the easy and quick way to implement tests, it has limits. I think it simply depends of what you need and that's a way amongst others.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.