Coder Social home page Coder Social logo

2022-test-existing-code's Introduction

How To Write Unit Tests For Existing Python Code

In this two-part miniseries, I show you a practical example of adding unit tests to existing code. This first part focuses on adding tests while not changing the original code. There are also a few things in the test setup that are not ideal, like how dates are used in the test code, using a real API key and doing actual card charges. I address these things in part 2, where I also show how refactoring the code simplifies test writing while improving the design as well.

2022-test-existing-code's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

2022-test-existing-code's Issues

test_api_key_invalid() does not raise a ValueError

Hi

First of all, thank You very much for this tutorial. I'm walking step by step and I suddenly stopped on: https://youtu.be/ULxMQ57engo?t=781 where this test does not throws me an error with empty API_KEY.
I'm getting an error like:

./before/tests/test_processor.py::test_api_key_invalid Failed: [undefined]Failed: DID NOT RAISE <class 'ValueError'>
def test_api_key_invalid() -> None:
>       with pytest.raises(ValueError):
E       Failed: DID NOT RAISE <class 'ValueError'>

before\tests\test_processor.py:8: Failed

I'm looking at the code of processor.py:PaymentProcessor class, and I can't see any code responsible for checking API_KEY which might throw an error. For a card number - yes there is a ValueError in charge() method, but nothing for API_KEY

Can I kindly ask You for some help/clarification? :) I just want to align your tutorial with my code

EDIT:
It's because on before/pay/processor.py class PaymentProcessor does not contain _check_api_key() method :) that's it.

TypeError: 'type' object is not subscriptable

collected 7 items / 3 errors

===================================================================================================== ERRORS ======================================================================================================
__________________________________________________________________________________ ERROR collecting pay/tests/test_line_item.py ___________________________________________________________________________________
pay/tests/test_line_item.py:1: in
from pay.order import LineItem
pay/order.py:22: in
class Order:
pay/order.py:23: in Order
line_items: list[LineItem] = field(default_factory=list)
E TypeError: 'type' object is not subscriptable
____________________________________________________________________________________ ERROR collecting pay/tests/test_order.py _____________________________________________________________________________________
pay/tests/test_order.py:1: in
from pay.order import LineItem, Order
pay/order.py:22: in
class Order:
pay/order.py:23: in Order
line_items: list[LineItem] = field(default_factory=list)
E TypeError: 'type' object is not subscriptable
___________________________________________________________________________________ ERROR collecting pay/tests/test_payment.py ____________________________________________________________________________________
pay/tests/test_payment.py:5: in
from pay.order import LineItem, Order, OrderStatus
pay/order.py:22: in
class Order:
pay/order.py:23: in Order
line_items: list[LineItem] = field(default_factory=list)
E TypeError: 'type' object is not subscriptable
============================================================================================= short test summary info =============================================================================================
ERROR pay/tests/test_line_item.py - TypeError: 'type' object is not subscriptable
ERROR pay/tests/test_order.py - TypeError: 'type' object is not subscriptable
ERROR pay/tests/test_payment.py - TypeError: 'type' object is not subscriptable
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 3 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Can anyone please help, I am getting TypeError when I run pytest command from refactored code directory. thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.