2

I have a test case like so:

class TokenGeneratorTestCase(unittest.TestCase):

        def test_genToken(self):
                """
                Test if genToken returns a UUID, if not, then -1
                """
                tg = TokenGenerator()
                result = tg.genToken()
                self.assertIn(type(result), [ uuid.UUID, int ])
                if type(result) == int:
                        self.assertEqual(result, -1)

I want to test if genToken() returns either a uuid.UUID or -1. Is it possible to do it with a single assert* ?

PS: I am new to both Python and Unit Testing, so forgive my ignorance :)

2
  • getToken() works completely randomly? Or generated value depends on some condition? Commented Nov 28, 2013 at 9:20
  • def genToken: if (someCondition): return -1 else: return uuid.uuid1() Commented Nov 28, 2013 at 9:24

2 Answers 2

3

There is nothing wrong in having two asserts as you are doing(assuming that you catch AssertionError :) ). Still if you want you can combine them like:

# You can also use type(result) == uuid.UUID or result == -1
correctness = isinstance(result, uuid.UUID) or (isinstance(result, int) and result == -1)
self.assertTrue(correctness)
Sign up to request clarification or add additional context in comments.

Comments

2

You have problem here - because you can't be sure if your TokenGenerator is working correctly. It can always return -1 and you will be happy that test is passing. You need to create two tests here - one which verifies generator returns -1 and one which verifies generator returns uuid.UUID. And these tests should be repeatable, not random. How you can do that? You should be able to affect someCondition in your token generator from test. I.e. you should pass some dependency to generator, or set appropriate generator state before you call genToken().

5 Comments

'someCondition' = isDiskSpaceAvailable(). How do I affect this?
@iceman you can create some class like Environment which will have method isDiskSpaceAvailable() and pass this environment object to token generator. This will allow you to mock Environment class, and provide your own results for isDisckSpaceAvailable() method. Actually I think token generator responsibility is generating token, not checking disk space on your machine.
Thanks for the clarification. But what if genToken() is the only consumer of isDiskSpaceAvailable(). Is it then justified to create a separate class?
@iceman you should create classes to group strongly related code together (i.e. class should have hight cohesion) - that's the reason of class creation. It doesn't matter how many consumers class will have. Good OOP design usually produces many small classes focused on specific responsibilities
@iceman one more point - unit tests should test classes in isolation from other classes and environment (FIRST)

Your Answer

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.