top of page

Testing

1. setting up Jenkins:


That's a first glimpse of what a build looks like in the end:

this runs every 5 minutes:

... and that's the Project, that is being build every time. Of course it's drawn from the github-repo each time.


Closer look on whether the tests worked out all fine (each test file is in bold):


C:\Program Files (x86)\Jenkins\workspace\Stats-Screening\Django_Project>manage.py test --verbosity=2 Creating test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')... test_whether_stock_append_to_portfolio (PortfolioScreen.Tests.test_objects.TestModels) ... ok test_whether_stock_ticker_is_in_portfolio (PortfolioScreen.Tests.test_objects.TestModels) ... ok test_project_about_GET (PortfolioScreen.Tests.test_views.TestViews) ... ok test_about_url_is_resolved (PortfolioScreen.Tests.test_urls.TestClass) ... ok test_chart_url_is_resolved (PortfolioScreen.Tests.test_urls.TestClass) ... ok test_main_url_is_resolved (PortfolioScreen.Tests.test_urls.TestClass) ... ok test_settings_url_is_resolved (PortfolioScreen.Tests.test_urls.TestClass) ... ok test_table_url_is_resolved (PortfolioScreen.Tests.test_urls.TestClass) ... ok test_equals (PortfolioScreen.Tests.test1.TestHelloWorld) ... ok test_secondTry (PortfolioScreen.Tests.test1.TestHelloWorld) ... ok ---------------------------------------------------------------------- Ran 10 tests in 0.033s OK


2. Writing some Tests in Python Django:


it is relevant that each test-file and test-method starts with the term "test". Those test-files have to be put into a folder which contains a __init__.py file. Otherwise the manage.py test-command which scans the entire project for tests won't find it. Sometimes it is required to delete other __init__.py-files on a higher level, so that those files (especially if they would start with test as well) don't get confused with the normal test-files.

when executing the manage.py test command (on the upper project level), each test-file is being executed one after another.


Command that needs to be executed:



The django.test-package is being used (based on the python unittest by the way). The tests are generally written as classes. Don't forget the test_ in front of every test-method.


There are 5 different topics which should be tested in Django:

1. Testing URLS

2. Testing Views

3. Testing Models /Testing Objects

4. Testing Forms

5. occasionally functional testing with Selenium (we skipped the last ones)


1. Testing URLS (test_urls.py) (excerpt - two examples, first file):


from django.test import SimpleTestCase from django.urls import reverse, resolve import unittest from ..views import PortfolioScreen,about,chart,table,settings class TestURLS(SimpleTestCase): def test_main_url_is_resolved(self): url = reverse('PortfolioScreen') print(resolve(url)) self.assertEquals(resolve(url).func, PortfolioScreen) def test_about_url_is_resolved(self): url = reverse('about') print(resolve(url)) self.assertEquals(resolve(url).func, about)


2. Testing Views (test_views.py - excerpt):

class TestViews(TestCase): def test_project_about_GET(self): client = Client() response = self.client.get(reverse('about')) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'PortfolioScreen/about.html')

3. Testing Models (test_models.py - only exert) -> still not written yet (too busy, too many errors and flaws of setting up Jenkins and other tests), will be done in the future


3. Testing Objects (test_objects.py) --> those are the python "container-beans". They will be eventually used for sticking them into the models.


class TestObjects(TestCase): allstocks = {'':''} def setUp(self): self.portfolio1 = Portfolio( 'Portfolio', '02-02-2015', ) stock1 = Stock('TSLA','725') stock1.addfigure( Figure('Alpha','x/a','12')) self.allstocks["TSLA"] = stock1 self.portfolio1.addStock(stock1) stock2 = Stock('APC','282') stock2.addfigure( Figure('Beta','x/b','13') ) self.allstocks["APC"] = stock2 self.portfolio1.addStock(stock2) def test_whether_stock_append_to_portfolio(self): self.assertDictEqual(self.portfolio1.stockList,self.allstocks) def test_whether_stock_ticker_is_in_portfolio(self): self.assertEqual(self.portfolio1.getStock('APC').ticker,'APC')



4. Testing Forms (also done in the future - code that could be tested is not on an extensive level yet)


3. Getting a Report in Jenkins


Been trying to that with XmlReport. No luck yet by now. Jenkins can't find or create the report file nor does it display "Report" as it's supposed to in the left nav-bar. Check lower part for Django.



Presuming that it would work:

1. Put this at the end of a testing file (of course import and pip install all the required):


if __name__ == '__main__': with open('/path/to/results.xml', 'wb') as output: unittest.main( testRunner=xmlrunner.XMLTestRunner(output=output), failfast=False, buffer=False, catchbreak=False)


2. Change the report path in project's configuration (select Junit report).


Maybe something to work on for the future if time is left over (which it will never be .... ). For now, the --verbosity=2 should be fine, since it displays the success of each test-method in detail on the Jenkins console.

Aktuelle Beiträge

Alle ansehen

Final Exam

Deployment & Tools: https://dhbw-karlsruhe.myjetbrains.com/youtrack/dashboard?id=ceebf9ae-0196-4858-b33c-d60a454df9ea Burndown-Charts https://dhbw-karlsruhe.myjetbrains.com/youtrack/reports/burndown/1

Deployment CI and Techstack

Testing: + Python django.test and Python unittest + Jenkins + Gherkins Development: + HTML + Plain JavaScript/jQuery + CSS/Bootstrap + Plain Python/Python Django + Github Deployment: + django

Installation

download from git-repo clear your whole server from any type of apache or other Web-Server-deployments currently running make sure your deployment server is running the latest version of python instal

bottom of page