Thursday Mornings with Python, part 3

Thursday Mornings with Python, part 3

Not starting out with the best start as having some issues with where I sit in the office, and also not feeling great from yesterday. But as I learned when I was in my early 20s, it's never too late to fix something you think that you have broken, so here I am, back to test fixtures.

Test Fixtures

Tests need to occur in a "known state". Conditions that are tests are run in need to be controlled. A test fixture is "a mechanism for ensuring proper test setup and test teardown.

Test fixtures "guarantee that our tests are running in predictable conditions, and thus the results are reliable." There is some stuff about @classmethod but again I am not doing it to death.

I don't think that we really use unittest in my team. 

But it is still really helpful to learn more about fixtures.

Really really helpful to understand them at last. After I have been using them a lot. I would like to know a bit more about them. 

This might be something to ask about. Although is that me being lazy? Let me do a bit more of the course and see. I am sorry.

I am looking forwards to the next part of the course anyway thank you. (In unittestn you can also change the argument passed into the test from self to cls but this is beginning to confuse me a bit so idk). Oh I get it - you pass it as an argument into the setup class method. ARRRGH. Which you use instead of just regular setup.

A pink infographic detailing aspects of the python course that I have done today with butterflies.

"It's generally good practice to create fixtures that run for every test. However, when a fixture has a large cost (i.e. it takes a long time), then it might make more sense to have it run once per test class rather than once per test." ... no idea... ...

I wrote some code... I have no idea what it does... but thanks yes yes... never mind 
import unittest
import kiosk

class CheckInKioskTests(unittest.TestCase):

  def test_check_in_with_flight_number(self):
    print('Testing the check-in process based on flight number')

  def test_check_in_with_passport(self):
    print('Testing the check-in process based on passport')

  @classmethod
  def setUpClass(cls):
    kiosk.power_on_kiosk()

  @classmethod
  def tearDownClass(cls):
    kiosk.power_off_kiosk()

  def setUp(self):
    kiosk.return_to_welcome_page()

unittest.main()

I wrote the three functions at the bottom - it links up with other folders but don't worry about it please too much thanks.

I use fixtures a lot at work and I am grateful to understand them better - might ask my colleague more about how we use them.

Skipping Tests

"Sometimes, we have tests that should only run in a particular context." Ooops right yes I see. 

I thought they were gonna teach me about only running certain tests at a time in the console - um yes - whoops.

In my NEMESIS unittest you can use either a method or a decorator to designate a skip test which is sooo cool.
  • @unittest skip decorator
  • skipTest() method
I would imagine that whatever form of pytest we use will have its equivlaent. 

But what do they do??!

Okay this is so so cool

I've just seen a really good example.
  • @unittest.skipUnless() will skip a test if certain condition evaluates to False
  • @unittest.skipIf() will skip a test if a certain condition evaluates to  True
  • Note that they have a decorator so I'm pretty sure this things are more or less just the same things as decorators anyway
Skip decorators are said to be slightly more convenient and this makes sense to me.
import unittest
import entertainment

class EntertainmentSystemTests(unittest.TestCase):

  @unittest.skipIf(entertainment.regional_jet(), 'Not available on regional jets')
  def test_movie_license(self):
    daily_movie = entertainment.get_daily_movie()
    licensed_movies = entertainment.get_licensed_movies()
    self.assertIn(daily_movie, licensed_movies)

  @unittest.skipUnless(entertainment.regional_jet() is False, 'Not available on regional jets')
  def test_wifi_status(self):
    wifi_enabled = entertainment.get_wifi_status()
    self.assertTrue(wifi_enabled)

  def test_device_temperature(self):
    if entertainment.regional_jet() is True:
      self.skipTest('Not available on regional jets')

    device_temp = entertainment.get_device_temp()
    self.assertLess(device_temp, 35)

  def test_maximum_display_brightness(self):
    if entertainment.regional_jet() is True:
      self.skipTest('Not available on regional jets')

    brightness = entertainment.get_maximum_display_brightness()
    self.assertAlmostEqual(brightness, 400)


unittest.main()

I wrote some of this code, added in the extra decorators. Again there is some other code related to this in the course but please don't worry about this too much please. Thanks!

Expected Failures

"Sometimes we have a test that we know will fail. This could happen when a feature has a known bug or is designed to fail on purpose." We don't want an expected failure to cloud our tests.

unittest allows us to mark tests as expected failures rather than just skipping them.

I don't know if this applies to what I work with or not but I am not too concerned. I will ask if I need to.

As with all of these lessons you learn a framework to only then go on and apply it to others.

"Expected failures are counted as passed in our test results." "If a test passes when we expected it to fail, then it is marked as failed in our test results." Oh wow I really didn't know this is so cool - wow.

unittest (I'm guessing) has it's own decorator for this.

@unittest.expectedFailure

I wonder how we do this for me and if and whether or not we even need to do this at all. 

I wrote in a tiny bit more code here, added just a tiny little decorator, here is the code and again it was linked to another file, here we go.
import unittest
import feedback

class CustomerFeedbackTests(unittest.TestCase):
  
  @unittest.expectedFailure
  def test_survey_form(self):
    self.assertEqual(feedback.issue_survey(), 'Success')

  def test_complaint_form(self):
    self.assertEqual(feedback.log_customer_complaint(), 'Success')

unittest.main()

Quiz

I got everything right until the final question. 😂 

It took me out! 

A test score of 91% showing from a unit testing quiz summary on codecademy.

I mixed up my decorator and my method but I know which is which... I just wasn't sure if I had seen unittest.skip as a decorator on its own before.

a question on unittest skip decorators that I got wrong in the quiz...

Now I'm moving on to the project which I didn't usually write too much on my blog about... edit: finished it! Yay.

Now I'm on to iterators and generators next week... finally. Starting with iterators. MY FAVE.

Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing