1

I am learning unit testing on the job, at a new job. The testing team is small, (1 other), and all of the tests are written in the following fashion.

class TestClass(unittest.TestCase):
    def testFunction(self):
        #Test - 1
        #Assign
        #Act
        #Assert

        #Test - 2
        #Assign
        #Act
        #Assert

        Cont...

So, asserting, then re-assigning the variables and asserting again.

Now, this is different from what I'm reading about unit tests online about unit tests (One test per method).

This makes it hard to debug tests because if there are say, 12 tests, and an error in the 12th, I have to step through 11 different tests to check the stack data and/or errors.

Is this normal?

Godron629
  • 131

1 Answers1

1

Is this normal?

Sadly, yes. Many don't see the benefit of writing one test per test class method and instead seem to focus on the speed of writing tests with multiple tests per method. Basically they take the easy way out and write things in a way that is easy to write, rather than easy to maintain.

How can we change this behavior (i.e. the unspoken question)? By writing the best tests we can so that, hopefully, other people will have to deal with our tests breaking in the future and then notice that it is easier to figure out what is happening when a method is doing only one test. I also tend to mention this during code review, although due to time constraints it is rarely implemented.

Maybe_Factor
  • 1,391
  • ___"Time. The enemy of us all."___ plus budget of course! – Steve Barnes May 18 '17 at 05:51
  • Indeed, Time is money (a.k.a budget). By using best practice testing, we can hope to save money and time in the future with more maintainable tests, rather than trying to save time in the present by writing quick and dirty tests. – Maybe_Factor May 19 '17 at 02:02
  • Unfortunately most program managers are more concerned with how much their project costs than saving the company money in the long term - I don't see a fix for this with the current accounting practices in most companies. – Steve Barnes May 19 '17 at 05:46