Python unittest - Ran 0 tests in 0.000s

2024/11/19 15:17:28

So I want to do this code Kata for practice. I want to implement the kata with tdd in separate files:

The algorithm:

# stringcalculator.py  
def Add(string):return 1

and the tests:

# stringcalculator.spec.py 
from stringcalculator import Add
import unittestclass TestStringCalculator(unittest.TestCase):def add_returns_zero_for_emptyString(self):self.assertEqual(Add(' '), 0)if __name__ == '__main__':unittest.main()

When running the testfile, I get:

Ran 0 tests in 0.000sOK

It should return one failed test however. What do I miss here?

Answer

As stated in the python unittest doc:

The simplest TestCase subclass will simply implement a test method(i.e. a method whose name starts with test)

So you will need to change your method name to something like this:

def test_add_returns_zero_for_emptyString(self):self.assertEqual(Add(' '), 0)
https://en.xdnf.cn/q/26422.html

Related Q&A

How and where does py.test find fixtures

Where and how does py.test look for fixtures? I have the same code in 2 files in the same folder. When I delete conftest.py, cmdopt cannot be found running test_conf.py (also in same fo…

Python match a string with regex [duplicate]

This question already has answers here:What exactly do "u" and "r" string prefixes do, and what are raw string literals?(7 answers)What exactly is a "raw string regex" an…

What are the consequences of disabling gossip, mingle and heartbeat for celery workers?

What are the implications of disabling gossip, mingle, and heartbeat on my celery workers?In order to reduce the number of messages sent to CloudAMQP to stay within the free plan, I decided to follow …

How to determine if an exception was raised once youre in the finally block?

Is it possible to tell if there was an exception once youre in the finally clause? Something like:try:funky code finally:if ???:print(the funky code raised)Im looking to make something like this m…

How to use re match objects in a list comprehension

I have a function to pick out lumps from a list of strings and return them as another list:def filterPick(lines,regex):result = []for l in lines:match = re.search(regex,l)if match:result += [match.grou…

How do I run pip on python for windows? [duplicate]

This question already has answers here:Why does "pip install" inside Python raise a SyntaxError?(6 answers)Closed 8 years ago.Ive just installed python 3.5, ran Python 3.5 (32-bit) and typed…

Select certain rows by index of another DataFrame

I have a DataFrame and I would select only rows that contain index value into df1.index. for Example: In [96]: df Out[96]:A B C D 1 1 4 9 1 2 4 5 0 2 3 5 5 1 0 22 1 3 9 6and these ind…

Execute .sql schema in psycopg2 in Python

I have a PostgreSQL schema stored in .sql file. It looks something like:CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY,facebook_id TEXT NOT NULL,name TEXT NOT NULL,access_token TEXT,created I…

AttributeError: module html.parser has no attribute HTMLParseError

This is the hints,how can I resolve it? I use Python 3.5.1 created a virtual envirement by virtualenv The source code works well on my friends computer machineError: Traceback (most recent call last):…

Error message python-pylint C0103:Invalid constant name

Im confused about the error(s) in this photo:I dont know how to fix them. My program is a Python-Flask web frame. When I use Visual Studio Code to debug my program, Pylint shows these errors. I know th…