This feed contains pages with tag "python".
Questions
- Time
- 5 Minutes
- Activity
- Group discussion
- Questions about Quiz 2?
Getting Started
Make a directory
~/cs2613/labs/L14
Download Work.zip and unzip in your newly created directory.
Mortgage Calculator
- Time
- 20 Minutes
- Activity
- Modify given program
Do exercises 1.7 and 1.8 from Python Numbers
Introduction to Python strings
- Time
- 20 Minutes
- Activity
- Experiment in Python REPL
Do exercises 1.13, 1.14, 1.15 from Python Strings
Pytest
- Time
- 10 minutes
- Activity
- Demo
In a terminal run
pipx install --include-deps unb-cs2613 rehash
Save the following sample code (based on Exercise 1.19) to a file
listex.py
in the directory you created above.
symbols = 'HPQ,AAPL,IBM,MSFT,YHOO,DOA,GOOG'
symlist = symbols.split(',')
def test_lookup0():
assert symlist[0] == 'HPQ'
def test_lookup1():
assert symlist[1] == 'AAPL'
In
~/cs2613/labs/L14
, runpytest listex.py
You should see
[student@id414m22 L14]$ pytest listex.py
=================== test session starts ===================
platform linux -- Python 3.9.18, pytest-7.4.3, pluggy-1.3.0
rootdir: /home1/ugrad/student/cs2613/labs/L14
plugins: pylama-8.4.1, cov-4.1.0
collected 2 items
listex.py .. [100%]
==================== 2 passed in 0.02s ====================
Modify one of the tests so it fails. Run pytest again.
Question for your journal: what are three ways failing tests are indicated?
Lists and Pytest
- Time
- 20 minutes
- Activity
- Individual programming from template
For each of the remaining interactive evaluations in Exercise 1.19, add a corresponding test to
listex.py
.make sure each
test_*
function (def
) has a unique name.if you want, you can have multiple asserts in a single test.
Questions for your journal:
- what are some differences between
==
in JavaScript and Python? - based on what you have seen so far, what are similarities and
differences between the
node
test framework andpytest
- what are some differences between
Functions and coverage
- Time
- 20 minutes
- Activity
- Individual programming, modify previous solution
We have already been using python functions for pytest
, without
really thinking about how they work. In Part 1.7 of Practical
Python,
functions are explained.
- Consider the sumcount function
def sumcount(n):
'''
Returns the sum of the first n integers
'''
total = 0
while n > 0:
total += n
n -= 1
return total
Make a recursive version of this function.
Add sufficient
pytest
tests for both versions of the function.Check your test(s) for coverage with
pytest --cov=sum --cov-report=term-missing sum.py
(assuming your file is called
sum.py
)