Welcome to Homework 2! In this unit, our goal is to establish a professional development setup for Python. This will include familiarizing yourselves with several key tools and practices that are integral to modern software development. All you need to do to complete this assignment is setup a new project that works just like mine in the video. You need to be able to run pytest, pylint, and coverage. You also need to read all the articles in the required section.
Specifically, you'll set up:
- Python Virtual Environments: Essential for managing project-specific dependencies.
- Pytest: A powerful framework for writing and running Python tests.
- Pylint: A tool for analyzing your Python code for errors and enforcing a coding standard.
- Coverage: A tool for measuring the coverage of your unit tests.
- Git: To practice version control techniques such as branching, merging, and using stash.
Additionally, we will delve into integrating these tools with Visual Studio Code (VSCode) and Windows Subsystem for Linux (WSL 2), enhancing your ability to manage development tasks.
Note: You should practice GIT and setting up a project more than one time, until you feel comfortable with all the commands. Even if you us VScode, you should still use the terminal for as many GIT commands as possible. Building a solid foundation with GIT will help you a lot in the long run
By the end of this homework, you should be able to:
- Set up a Python virtual environment and manage dependencies.
- Use
pip freeze
to create arequirements.txt
file. - Configure and utilize pytest, pylint, and coverage in a Python project.
- Apply git commands for effective version control.
- Integrate VSCode with WSL 2 for a streamlined development process.
Understanding the broader context of these tools and methodologies is crucial. Please read the following materials to gain insights into their professional application:
- Automated Testing at Google: A case study on the importance of automated testing in software development. Read here.
- Agile Manifesto: The foundation of Agile software development, highlighting the role of automated testing. Read here.
- The 12 Factor App: A guide to best practices in maintaining mission-critical software, relevant to our course. Read here.
- Continuous Integration: An article explaining the practice of continuous testing and deployment, essential for Agile implementation. Read here.
You need to do the following for this assignment:
- Install python using the Mac or Ubuntu instructions below. You will only do this step once for the course on your commputer. Mac needs Brew package manager to install Python and Ubuntu needs to just run the command:
sudo apt update -y
sudo apt install python3-pip
pip3 --version
- Install python virtual environment, so you can manage the virtual environment to issolate your dependences from the global version of python. You need to do this once for your computer, this is a global python package.
pip3 install virtualenv
note You should try the following to test your install with my project, after you have installed python and the virtual environment manager:
git clone git@github.com:kaw393939/git_python_testing_setup_homework.git
cd git_python_testing_setup_homework
source venv/bin/activate
pip3 install -r requirements.txt
pytest --pylint --cov
- Now Make a project directory not inside my own project if you cloned mine. DO NOT CREATE A NEW REPO INSIDE OF THE ONE YOU CLONE FROM ME OR YOU WILL HAVE PROBLEMS, setup the virtual environment in the directory that you make for your projectand activate it. For more information see here. You need to do this for each new python project.
cd .. <-- goes up a directory to get out of the project you cloned to test your install of python. Make sure you do a pwd to see where your going to make your project from scratch.
mkdir myproject
cd myproject
virtualenv venv <- Makes a virtual environment in the venv directory (you can make this any directory but you will have to remember it to activate it correctlty)
source ./venv/bin/activate <- you should see (venv) in the terminal command line to indicate that the venv environment of your project is activated
- Once the virtual environment is a active then install the python dependencies using pip:
pip3 install pytest pytest-pylint pytest-cov
- Once you have the libraries installed you need to freeze the requiremnts and create your requirments.txt file. You need to do this so that the versions of the libaries / dependencies you use are saved, so that your project can be installed somewhere else.
pip3 freeze > requirements.txt
Note When someone copies / clones your repository they will install the specfic library / dependency requirements for your project using the command:
pip3 install -r requirments.txt
-
Once you have this done, you will need to create the "tests" and "calculator" folders and add the init.py file to each folder. You need a .gitignore with the same contents as mine, you need the .pylintrc file, and the pytest.ini file. You will need to put the same code that I have in those files.
-
Once you have the files made make sure you have the code that I have in my calculator folder's init.py file in yours and add the code for your test to the test_calculator.py file. Essentially, you should have the same files as what I have in this repository.
-
Run the tests:
pytest <-runs the tests without pylint or coverage
pytest --pylint <- Runs tests with pylint static code analysis
pytest --pylint --cov <-Runs tests, pylint, and coverage to check if you have all your code tested.
- Once you have a new project setup and everything working, just submit a link to your repository to Canvas.
I recommend that you go through this interactive tutorial on GIT to learn as much as you can about the command line operation of GIT:
Here are some fundamental Git commands you'll be using:
- Add a file to staging:
git add .gitignore
- Commit changes:
git commit -m "added git ignore with sw* files"
- Check status and staging:
git status
- Update local branch with remote changes:
git pull --rebase origin main
- Initial Python Setup: Ubuntu Guide
- Install the Python virtual environment manager here
- Install Brew Package Manager: Brew Installation
- Set Up Python Virtual Environments with Brew: Mac Guide
Note: Once virtual environments are set up on Mac, you can follow the same instructions as for Ubuntu.
These readings will help you understand the tools you'll be using:
- Pip Freeze for Beginners: Read here
- Introduction to Pytest: Read here
- Pylint Documentation: Read here
- Pytest Documentation: Read here
To efficiently open and manage your projects with VSCode instead of the terminal, follow this guide:
Reminder: Practice and understanding are key. Don’t hesitate to reach out if you have questions or need further clarifications. Happy coding!