Clean up tests and migrate (#540)

* migrate
  * abstract base class test
  * remove assert_equal in test_cli
  * remove nose from test_cli
  * remove nose dependencies
  * use tempfile
* configure coverage
  * flag python version in name
  * enable comment
* drop EOL py37
This commit is contained in:
Patrick Kanzler 2023-08-10 00:18:02 +02:00 committed by GitHub
parent 31daabcbea
commit 4c2dcdfac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 61 additions and 53 deletions

View File

@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11']
steps:
- uses: actions/checkout@v3
@ -41,3 +41,13 @@ jobs:
tox
env:
ESCPOS_CAPABILITIES_FILE: /home/runner/work/python-escpos/python-escpos/capabilities-data/dist/capabilities.json
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
directory: ./coverage/reports/
env_vars: OS,PYTHON
fail_ci_if_error: true
files: ./coverage.xml,!./cache
flags: unittests
name: coverage-tox-${{ matrix.python-version }}
verbose: true

2
.gitignore vendored
View File

@ -22,6 +22,7 @@ dist/
src/escpos/version.py
.hypothesis
.pytest_cache/
coverage.xml
# pyenv
.python-version
@ -40,3 +41,4 @@ test/test-cli-output/
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

View File

@ -52,7 +52,6 @@ We use basically standard rst-docstrings for Sphinx.
Test
^^^^
Try to write tests whenever possible. Our goal for the future is 100% coverage.
We are currently using `nose` but might change in the future.
You can copy the structure from other testcases. Please remember to adapt the docstrings.
Further reading

View File

@ -8,5 +8,10 @@ coverage:
changes: off
range: "60...100"
comment: off
comment:
layout: " diff, flags, files"
behavior: default
require_changes: false # if true: only post the comment if coverage changes
require_base: false # [true :: must have a base report to post]
require_head: true # [true :: must have a head report to post]

View File

@ -5,7 +5,7 @@ description = Python library to manipulate ESC/POS Printers
long_description = file: README.rst
license = MIT
license_file = LICENSE
author = Manuel F Martinez and others
author = python-escpos developers
author_email = dev@pkanzler.de
maintainer = Patrick Kanzler
maintainer_email = dev@pkanzler.de
@ -18,7 +18,6 @@ classifiers =
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
@ -56,17 +55,12 @@ tests_require =
pytest!=3.2.0,!=3.3.0
pytest-cov
pytest-mock
nose
scripttest
mock
hypothesis>4
flake8
sphinxcontrib-spelling>=7.2.0
[nosetests]
verbosity=3
with-doctest=1
[flake8]
exclude = .git,.tox,.github,.eggs,__pycache__,doc/conf.py,build,dist,capabilities-data,test,src/escpos/constants.py
max-line-length = 120

View File

@ -7,17 +7,16 @@
:license: MIT
"""
from nose.tools import raises
import pytest
import escpos.escpos as escpos
from abc import ABCMeta
@raises(TypeError)
def test_abstract_base_class_raises():
"""test whether the abstract base class raises an exception for ESC/POS"""
escpos.Escpos() # This call should raise TypeError because of abstractmethod _raw()
with pytest.raises(TypeError):
# This call should raise TypeError because of abstractmethod _raw()
escpos.Escpos()
def test_abstract_base_class():

View File

@ -4,26 +4,25 @@
import os
import sys
from scripttest import TestFileEnvironment
from nose.tools import assert_equal, nottest
import pytest
from scripttest import TestFileEnvironment as TFE
import tempfile
import shutil
import escpos
TEST_DIR = os.path.abspath("test/test-cli-output")
TEST_DIR = tempfile.mkdtemp() + "/cli-test"
DEVFILE_NAME = "testfile"
DEVFILE = os.path.join(TEST_DIR, DEVFILE_NAME)
CONFIGFILE = "testconfig.yaml"
CONFIG_YAML = """
CONFIG_YAML = f"""
---
printer:
type: file
devfile: {testfile}
""".format(
testfile=DEVFILE,
)
devfile: {DEVFILE}
"""
class TestCLI:
@ -39,13 +38,14 @@ class TestCLI:
def teardown_class(cls):
"""Remove config file"""
os.remove(CONFIGFILE)
shutil.rmtree(TEST_DIR)
def setup_method(self):
"""Create a file to print to and set up env"""
self.env = None
self.default_args = None
self.env = TestFileEnvironment(
self.env = TFE(
base_path=TEST_DIR,
cwd=os.getcwd(),
)
@ -77,9 +77,11 @@ class TestCLI:
"""Test the version string"""
result = self.env.run("python-escpos", "version")
assert not result.stderr
assert_equal(escpos.__version__, result.stdout.strip())
assert escpos.__version__ == result.stdout.strip()
@nottest # disable this test as it is not that easy anymore to predict the outcome of this call
@pytest.mark.skip(
reason="disable this test as it is not that easy anymore to predict the outcome of this call"
)
def test_cli_text(self):
"""Make sure text returns what we sent it"""
test_text = "this is some text"
@ -95,15 +97,15 @@ class TestCLI:
)
assert not result.stderr
assert DEVFILE_NAME in result.files_updated.keys()
assert_equals(result.files_updated[DEVFILE_NAME].bytes, test_text + "\n")
assert result.files_updated[DEVFILE_NAME].bytes == test_text + "\n"
def test_cli_text_inavlid_args(self):
def test_cli_text_invalid_args(self):
"""Test a failure to send valid arguments"""
result = self.env.run(
*(self.default_args + ("text", "--invalid-param", "some data")),
expect_error=True,
expect_stderr=True
expect_stderr=True,
)
assert_equal(result.returncode, 2)
assert result.returncode == 2
assert "error:" in result.stderr
assert not result.files_updated

View File

@ -1,4 +1,3 @@
from nose.tools import assert_raises
from escpos.printer import Dummy

View File

@ -8,7 +8,6 @@
"""
from nose.tools import raises
import pytest
import escpos.printer as printer
@ -66,25 +65,25 @@ def test_model():
assert instance.output == expected
@raises(ValueError)
def test_invalid_ec():
"""Test invalid QR error correction"""
instance = printer.Dummy()
instance.qr("1234", native=True, ec=-1)
with pytest.raises(ValueError):
instance.qr("1234", native=True, ec=-1)
@raises(ValueError)
def test_invalid_size():
"""Test invalid QR size"""
instance = printer.Dummy()
instance.qr("1234", native=True, size=0)
with pytest.raises(ValueError):
instance.qr("1234", native=True, size=0)
@raises(ValueError)
def test_invalid_model():
"""Test invalid QR model"""
instance = printer.Dummy()
instance.qr("1234", native=True, model="Hello")
with pytest.raises(ValueError):
instance.qr("1234", native=True, model="Hello")
@pytest.mark.skip("this test has to be debugged")
@ -103,11 +102,11 @@ def test_image():
assert instance.output == expected
@raises(ValueError)
def test_image_invalid_model():
"""Test unsupported QR model as image"""
instance = printer.Dummy()
instance.qr("1234", native=False, model=QR_MODEL_1)
with pytest.raises(ValueError):
instance.qr("1234", native=False, model=QR_MODEL_1)
@pytest.fixture

View File

@ -1,4 +1,4 @@
from nose.tools import assert_raises
import pytest
from escpos.printer import Dummy
@ -16,11 +16,11 @@ def test_line_spacing_rest():
def test_line_spacing_error_handling():
printer = Dummy()
with assert_raises(ValueError):
with pytest.raises(ValueError):
printer.line_spacing(99, divisor=44)
with assert_raises(ValueError):
with pytest.raises(ValueError):
printer.line_spacing(divisor=80, spacing=86)
with assert_raises(ValueError):
with pytest.raises(ValueError):
printer.line_spacing(divisor=360, spacing=256)
with assert_raises(ValueError):
with pytest.raises(ValueError):
printer.line_spacing(divisor=180, spacing=256)

View File

@ -10,7 +10,6 @@
import pytest
from nose.tools import raises, assert_raises
from hypothesis import given, example
import hypothesis.strategies as st
from escpos.magicencode import MagicEncode, Encoder
@ -32,9 +31,9 @@ class TestEncoder:
assert not Encoder({"CP437": 1}).find_suitable_encoding("")
assert Encoder({"CP858": 1}).find_suitable_encoding("") == "CP858"
@raises(ValueError)
def test_get_encoding(self):
Encoder({}).get_encoding_name("latin1")
with pytest.raises(ValueError):
Encoder({}).get_encoding_name("latin1")
class TestMagicEncode:

View File

@ -1,5 +1,5 @@
[tox]
envlist = py37, py38, py39, py310, py311, docs, flake8
envlist = py38, py39, py310, py311, docs, flake8
[gh-actions]
python =
@ -10,10 +10,10 @@ python =
3.9: py39
3.10: py310
3.11: py311
3.12: py312
[testenv]
deps = nose
jaconv
deps = jaconv
coverage
scripttest
mock
@ -22,7 +22,7 @@ deps = nose
pytest-mock
hypothesis>4
python-barcode
commands = pytest --cov escpos
commands = pytest --cov escpos --cov-report=xml
passenv = ESCPOS_CAPABILITIES_PICKLE_DIR, ESCPOS_CAPABILITIES_FILE, CI, TRAVIS, TRAVIS_*, APPVEYOR, APPVEYOR_*, CODECOV_*
[testenv:docs]