kevgathuku / pytest-intro

Getting started with pytest
https://semaphoreci.com/community/tutorials/testing-python-applications-with-pytest
MIT License
12 stars 61 forks source link

Questions about your blog: https://semaphoreci.com/community/tutorials/testing-python-applications-with-pytest #1

Open alphaCTzo7G opened 6 years ago

alphaCTzo7G commented 6 years ago

Hi Kevin,

Thanks for this great article. It gave me a great understanding of pytest, parameterizing pytest and creating fixtures.

Question 1:

There is a difference between import-ing and running a function in a python module, at the ipython command prompt and using the %run/run to run a module at the command prompt, in how python sets the sys.path, and hence whether certain modules will be imported and whether the test function will work or not.

In the case you pointed, out capital_case and test_capital_case are in the same module. Similarly, wallet.py and test_wallet.py are in the same directory. So regardless of whether pytest import -s or run-s the functions in the test_wallet module, it shouldn’t have problems. However, if your test_module.py and module.py are located in different directories, and depending on the directory structure where they are located a run statement and import statement will see different sys.path and could thus fail.

So I know how to write the import statements, if the test_module and module are located in different directories, I wonder if pytest actually does something similar to a run test_module.py or more like import test_module.test_function()?

Question2:

I noticed that even though the boilerplate code did decrease, when you used test fixtures, the total number of lines of code actually increased. Are test fixtures primarily used when you have larger libraries? How do you decide when to start using test-fixtures?

Question3:

I noticed that you have a module named wallet, a class named Wallet and a variable named wallet. I may be wrong, but wouldnt it be better to have different names, or is this best practice?

Potential Correction:

I noticed that in the following function, there is an extra space before return:

def capital_case(x):
    if not isinstance(x, str):
        raise TypeError('Please provide a string argument')
      return x.capitalize()

Perhaps thats a typo. I thought it would be helpful if I pointed it out so, its slightly more clear for new python coders.

Thanks again for writing this tutorial!

kevgathuku commented 6 years ago

Hey @alphaCTzo7G thanks for the compliment. Glad to hear you enjoyed the post! Moving on to the questions:

  1. I'm not sure I understand the question / whether I am capable of answering it. If you're referring to the test and application layout/structure, you can get more insight into the question / potential solutions by reading this section of the Pytest docs If this still doesn't answer the question, please add a comment clarifying more on the exact question, then I'll see how I can help.

  2. I noticed that even though the boilerplate code did decrease, when you used test fixtures, the total number of lines of code actually increased. Are test fixtures primarily used when you have larger libraries? How do you decide when to start using test-fixtures?

I'm going to give a very generic answer here: It depends 😃 It's all a matter of using what works best for you. If the fixtures help you understand the tests better / increase your productivity, then use more of them, if not, use less / none of them. It all boils down to a mix of personal preference and the needs of the project.

  1. I noticed that you have a module named wallet, a class named Wallet and a variable named wallet. I may be wrong, but wouldnt it be better to have different names, or is this best practice?

My naming here is is horribly wrong 🤦‍♂️ It is much better to have different names that describe the different items as closely as possible. This was a huge mistake on my part. Thanks for pointing it out.

And in the capital_case function, that was indeed a typo.

Thanks for all the feedback @alphaCTzo7G 👍