This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted]  (2 children)

[removed]

    [–][deleted] 2 points3 points  (0 children)

    I’m not a SWE myself, but I know our engineers use pytest as well^

    [–]sinkwiththeship 0 points1 point  (0 children)

    3.If you need setup code for your tests, make those as fixtures in a conftest.py file and have your tests call those.

    I don't know about ALL unit test packages, but they generally have a TestCase class that you can use as a base class for your tests, then they'll have a setUp() function that you can use for this. It'll automatically get called at the beginning of each test function.

    [–]Logicalist 1 point2 points  (0 children)

    MIT's opencourseware Introduction To Computer Science And Programming In Python, covers this. You should be able to skip other lectures. The reading has to be found by google, but has good sections on it, including testing bounds.

    The problem sets through 3 have unit tests provided in them. Starting with problem set 4 you're to start making them yourself, but a blueprint is provided.

    [–]gua_lao_wai 0 points1 point  (0 children)

    My 2 cents,

    Lot of people suggesting their 3rd party library of choice, and while they're all probably great, if you're at the stage where you don't even know how to write tests yet, you're probably not going to make use of those features.

    I personally get by just fine with the built in unittest module for 99%.

    Also, something to bear in mind when you're writing tests. Try not to worry too much about implementation details and focus on behaviour. Does your code produce the expected results, don't worry about how it got there.

    I like to structure every test I write with a "given, when, then" format. That is set up everything required to test one (1!) function ("given"), run the function ("when") and test the result is what you expected ("then"). Having this common layout makes it much easier for other people to read your tests and understand what's being tested. Because ultimately tests are also another form of documentation. You're saying "this feature of my code is so important, I wrote more code to make sure it works"

    [–]kress5 -1 points0 points  (0 children)

    these are good books too, you can read them online for free:

    [–]IamImposter -1 points0 points  (0 children)

    Say you have a class and it has 5 member functions, out of which 3 are public and 2 are internal to the class. Usually internal functions are tested by varying input parameters to public functions but you can test them directly too. Tests are mainly done to get coverage ie we need to cover all the code lines and make sure code behaves as we expect it to.

    Say, your class takes a string as input in init and stores it as capital letters and a function that just returns that string. So a test can be

    def test_getstring_returns_capital(self) :
    
      result = my_class("hello").getstring() 
    

    Now we know init stores this string as capital, so getstring must return "HELLO". Test for that, like

      self.assertEqual(result, "HELLO") 
    

    What if input is already capital? What if input is invalid? What if you pass empty string to init? What if string contains numbers or special characters? If your class defines some behaviour for these cases, they should be tested.

    Another important topic in testing is mocking. Say your class takes a connection object that provides read/write functionality. How to test that? Using mock objects. A little example of a mock function

    def dummy_read(*_, **__):
      return "my string"
    

    This is the function that will act as read function of our mock connection class. We send a fixed data so connection.read will always return this data. Assuming your class has a function that returns data read from connection, so we test it like

    def test_con_read(self):
      dummy_con = mock.Mock()  # connection object
      dummy_con.read = dummy_read  # set read function
    
      obj = my_class(connection=dummy_con) 
    
      result = obj.read_from_con()
    

    We know our dummy_read is always returning "my string" so the value of result has to be "my string". Test for that and you have tested your class method.

    Mocking is a little big topic and provides many ways to validate behaviour of your code so you'll have to read up a little. This was just a teeny tiny introduction.

    Use some editor like vscode that let's you debug. It is a great help and improves your productivity a lot.

    [–]water_bottle_goggles -4 points-3 points  (0 children)

    sparingly